Print list of lists, Prolog - list

I got this grid:
tab([[s,f,f,f,s,f,f,f,s],
[f,s,f,f,f,f,f,s,f],
[f,f,s,f,f,f,s,f,f],
[f,f,f,f,f,f,f,f,f],
[s,f,f,f,m,f,f,f,s],
[f,f,f,f,f,f,f,f,f],
[f,f,s,f,f,f,s,f,f],
[f,s,f,f,f,f,f,s,f],
[s,f,f,f,s,f,f,f,s]]).
I want to print in the screen without brackets and commas.
By the way I can't print it right with or without them.
These are the print rules:
viewTab([]).
viewTab([H|T]) :-
printList(H),
viewTab(T).
printList([]) :-
nl.
printList([H|T]) :-
write(H),
write(' | '),
printList(T).
I call it in the Prolog's terminal like:
?- viewTab(X), tab(X).
I can't print a thing, and I get an infinite loop at:
printList([]) :-
nl.
Can you help me find my mistake?
Or some tips to make the code easier to work with.

Your viewTab/1 is not a purely logical predicate: it has a side effect, and it does not terminate for if its argument is a variable.
For example:
?- listing(foo).
foo([]).
foo([_|A]) :-
foo(A).
true.
?- foo(X).
X = [] ;
X = [_G256] ;
X = [_G256, _G259] ;
X = [_G256, _G259, _G262] ;
X = [_G256, _G259, _G262, _G265] ;
X = [_G256, _G259, _G262, _G265, _G268] . % and so on
So this:
?- viewTab(X), tab(X).
Puts a list in X, then tab(X) fails, and you are back at viewTab(X), ad infinitum.
You should try:
?- tab(X), viewTab(X).

Use dcg!
Definite clause grammars are a versatile, logical way of processing input/output.
For a start, read this well-written DCG primer by Markus Triska, also known as #mat on SO!
Right now, as a quick fix, use the built-in predicate format/2 like this:
?- X = [a,b,c], format('~s~n',[X]).
abc % output via side-effect
X = [a, b, c]. % query succeeds

Related

Prolog Convert a list in a list of lists

I need to convert a list of elements into a list of lists.
For example, if i have the list [1,2,3,4] the output must be [[1],[2],[3],[4]], one element per list.
create([],_, _, _).
create([H|T], Aux, X, Result) :-
append([H], Aux, X),
Result = [X],
create(T, X, _, Result).
I always get false... is this even possible to do?
Another possibility to define this relation is by using DCGs. They yield easily readable code when describing lists. Let's stick with the name singletons as suggested by #false in the comments:
singletons([]) --> % the empty list
[]. % is empty
singletons([H|T]) --> % the head of a nonempty list
[[H]], % is a list on its own
singletons(T). % and so is the tail
You can query this directly with phrase/2:
?- phrase(singletons([1,2,3,4]),X).
X = [[1],[2],[3],[4]]
Or write a wrapper-predicate with phrase/2 as the single goal:
singletons(L,Ls) :-
phrase(singletons(L),Ls).
And query that:
?- singletons([1,2,3,4],Ls).
Ls = [[1],[2],[3],[4]]
The predicate also works the other way around:
?- singletons(L,[[1],[2],[3],[4]]).
L = [1,2,3,4] ? ;
no
As well as the most general query:
?- singletons(L,Ls).
L = Ls = [] ? ;
L = [_A],
Ls = [[_A]] ? ;
L = [_A,_B],
Ls = [[_A],[_B]] ?
...
Alternatively you can also define a simple predicate that describes a relation between an arbitrary element and itself in brackets and then use maplist/3 from library(apply) to apply it on every element of a list:
:- use_module(library(apply)).
embraced(X,[X]).
singletons(L,Ls) :-
maplist(embraced,L,Ls).
This version yields the same results for the above queries. However, it is more efficient. To see that consider the following query from above:
?- singletons(L,[[1],[2],[3],[4]]).
L = [1,2,3,4]
Above you had to enter ; to make Prolog search for further solutions and subsequently fail (indicated by no). With this version there are no unnecessary choice points left and Prolog is succeeding deterministically for the query.
Try this
create([],[]).
create([H|T],[[H]|T2]):- create(T,T2).
I tried
?- create([1,2,3,4],X).
and the result was
X = [[1], [2], [3], [4]].

Palindrome (homework)

I tried to write a Prolog program by using lists. However, I have to use difference lists and output should be:
The ith element of the list is the same of (n-i+1)th element of the list and n is the length of the list. For example, [a,X,c,b,Y] should give X = b and Y = a. I could not find similar palindrome example in other questions.
So far I have implemented:
% length of the list
len([], 0).
len([H|T], B) :-
len(T, NT),
B is NT + 1.
% return the ith element of the list
match([H|_], 0, H) :-
!.
match([_|T], N, H) :-
N > 0,
N1 is N-1,
match(T, N1, H).
However, I could not complete. Please help me!
Use definite clause grammars!
DCG, a major Prolog feature, makes using difference lists easy—enabling you to write concise and efficient code with little effort!
Want to know more? Just follow the dots:
DCG has its own tag on StackOverflow, dcg.
en.wikipedia.org has an extensive article on DCG.
For a jumpstart, read the DCG primer by Markus Triska!
Without any further ado, let's get to the code:
palindrome --> [].
palindrome --> [_].
palindrome --> [X], palindrome, [X].
% Alternatively, we could also use the following more compact definition:
palindrome --> [] | [_] | [X], palindrome, [X].
Done. Let's run a few queries! First, the query the OP gave:
?- phrase(palindrome, [a,X,c,b,Y]).
X = b, Y = a
; false.
In German, "corn" is called "mais". If we put "siam" (the old name of "the Kingdom of Thailand") in front, we get a delicious palindrome:
?- set_prolog_flag(double_quotes, chars).
true.
?- phrase(palindrome, "siammais").
true
; false.
?- phrase(palindrome, "siamais"). % or kick one middle 'm' character
true % ... for an odd-length palindrome
; false.
At last, let's not forget about the most general query:
?- phrase(palindrome, Xs).
Xs = []
; Xs = [_A]
; Xs = [_A,_A]
; Xs = [_A,_B,_A]
; Xs = [_A,_B,_B,_A]
; Xs = [_A,_B,_C,_B,_A]
...
On the prolog-toplevel we can use the built-in Prolog predicate listing/1 to peek at the code the DCG was "translated" to—at this level the internal use of difference-lists becomes apparent:
?- listing(palindrome//0).
palindrome(A, A).
palindrome([_|A], A).
palindrome([C|A], D) :-
palindrome(A, B),
B = [C|D].
true.

Prolog: How "length(+,-)" delete unassigned tail of the list keeping the list?

Again a Prolog beginner :-}
I build up a list element by element using
(1)
member(NewElement,ListToBeFilled).
in a repeating call,
(2)
ListToBeFilled = [NewElement|TmpListToBeFilled].
in a recursive call like
something(...,TmpListToBeFilled).
A concrete example of (2)
catch_all_nth1(List, AllNth, Counter, Result) :-
[H|T] = List,
NewCounter is Counter + 1,
(
0 is Counter mod AllNth
->
Result = [H|Result1]
;
Result = Result1
),
catch_all_nth1(T,AllNth,NewCounter,Result1),
!.
catch_all_nth1([], _, _, _).
As result I get a list which looks like
[E1, E2, E3, ..., Elast | _G12321].
Of course, the Tail is a Variable. [btw: are there better method to fill up the
list, directly avoiding the "unassigned tail"?]
I was now looking for a simple method to eliminate the "unassigned tail".
I found:
Delete an unassigned member in list
there it is proposed to use:
exclude(var, ListWithVar, ListWithoutVar),!,
[Found this too, but did not help as I do not want a dummy element at the end
Prolog list has uninstantiated tail, need to get rid of it ]
What I noticed is that using length\2 eliminate the "unassigned tail", too, and in addtion
the same List remains.
My Question is: How does it work? I would like to use the mechanism to eliminate the unassigned tail without using a new variable... [in SWI Prolog 'till now I did not get the debugger
entering length() ?!]
The example:
Z=['a','b','c' | Y],
X = Z,
write(' X '),write(X),nl,
length(X,Tmp),
write(' X '),write(X),nl.
13 ?- test(X).
X [a,b,c|_G3453]
X [a,b,c]
X = [a, b, c] .
I thought X, once initialized can not be changed anymore and you need
a new variable like in exclude(var, ListWithVar, ListWithoutVar).
Would be happy if someone explain the trick to me...
Thanks :-)
You're right about the strange behaviour: it's due to the ability of length/2 when called with unbound arguments
The predicate is non-deterministic, producing lists of increasing length if List is a partial list and Int is unbound.
example:
?- length([a,b,c|X],N).
X = [],
N = 3 ;
X = [_G16],
N = 4 ;
X = [_G16, _G19],
N = 5 ;
...
For your 'applicative' code, this tiny correction should be sufficient. Change the base recursion clause to
catch_all_nth1([], _, _, []).
here are the results before
4 ?- catch_all_nth1([a,b,c,d],2,1,R).
R = [b, d|_G75].
and after the correction:
5 ?- catch_all_nth1([a,b,c,d],2,1,R).
R = [b, d].
But I would suggest instead to use some of better know methods that Prolog provide us: like findall/3:
?- findall(E, (nth1(I,[a,b,c,d],E), I mod 2 =:= 0), L).
L = [b, d].
I think this should do it:
% case 1: end of list reached, replace final var with empty list
close_open_list(Uninstantiated_Var) :-
var(Uninstantiated_Var), !,
Uninstantiated_Var = '[]'.
% case 2: not the end, recurse
close_open_list([_|Tail]) :-
close_open_list(Tail).
?- X=[1,2,3|_], close_open_list(X).
X = [1, 2, 3].
Note that only variable X is used.. it simply recurses through the list until it hits the var at the end, replaces it with an empty list, which closes it. X is then available as a regular 'closed' list.
Edit: once a list element has been assigned to something specific, it cannot be changed. But the list itself can be appended to, when constructed as an open list ie. with |_ at the end. Open lists are a great way to build up list elements without needing new variables. eg.
X=[1,2,3|_], memberchk(4, X), memberchk(5,X).
X = [1, 2, 3, 4, 5|_G378304]
In the example above, memberchk tries tries to make '4', then '5' members of the list, which it succeeds in doing by inserting them into the free variable at the end in each case.
Then when you're done, just close it.

Prolog how to swap two by two elements in a list function?

I need a function in Prolog: swapcouple(L, L1).
swapcouple([a,b,c,d,e], M) --> output M=[b,a,d,c,e]
swapcouple([a,b,c,d], M) --> output M=[b,a,d,c]
(what have you tried?) This is a valid definition:
swapcouple([a,b,c,d,e], M) :- M=[b,a,d,c,e].
swapcouple([a,b,c,d], M) :- M=[b,a,d,c].
Proceed by abstraction. For example,
swapcouple([A,B,C,D,E], M) :- M=[B,A,D,C,E].
swapcouple([A,B,C,D], M) :- M=[B,A,D,C].
Do you see where I'm going? [A,B,C,D,E] = [A,B | R] where R = [C,D,E]. Can we use that?
swapcouple([A,B|R], M) :- R=[C,D,E], M=[B,A|S], S=[D,C,E].
Right? Here's the crucial bit. R=[C,D,E], S=[D,C,E] is the same as swapcouple(R,S), isn't it?
swapcouple([A,B|R], M) :- M=[B,A|S], swapcouple(R,S).
Assuming that swapcouple does what it is advertised to do, we can just use it when the need arises. Here you've got your very own recursive procedure (well, predicate). It is even tail recursive modulo cons, which is even more hip and fun.
Few more edge cases are missing there. I'm positive you can finish it up.
The implementation can hardly get more direct than this:
list_swappedcouples([],[]).
list_swappedcouples([A],[A]).
list_swappedcouples([A,B|Xs],[B,A|Ys]) :-
list_swappedcouples(Xs,Ys).
Here are your sample queries:
?- list_swappedcouples([a,b,c,d,e],Ls).
Ls = [b,a,d,c,e] ; % succeeds, but leaves behind choicepoint
false.
?- list_swappedcouples([a,b,c,d],Ls).
Ls = [b,a,d,c]. % succeeds deterministically
Edit 2015-06-03
We can utilize first argument indexing to improve determinism.
list_with_swapped_couples([],[]).
list_with_swapped_couples([X|Xs],Ys) :-
list_prev_w_swapped_couples(Xs,X,Ys).
list_prev_w_swapped_couples([],X,[X]).
list_prev_w_swapped_couples([X1|Xs],X0,[X1,X0|Ys]) :-
list_with_swapped_couples(Xs,Ys).
Note that all following sample queries succeed deterministically.
?- list_with_swapped_couples([],Xs).
Xs = [].
?- list_with_swapped_couples([1],Xs).
Xs = [1].
?- list_with_swapped_couples([1,2],Xs).
Xs = [2,1].
?- list_with_swapped_couples([1,2,3],Xs).
Xs = [2,1,3].
?- list_with_swapped_couples([1,2,3,4],Xs).
Xs = [2,1,4,3].
?- list_with_swapped_couples([1,2,3,4,5],Xs).
Xs = [2,1,4,3,5].

Prolog element in lists replacement

Hi i was wondering if you could help me out with this
From programming in Prolog: write Prolog script for replacement any given element in lists by an another given element. For example:
replace( 3, a,[1,2,3,4,3,5], [1,2,a,4,a,5])=true
Many Thanks in advance
In Prolog, most list processing is done by processing the head and then recursively processing the rest of the list. Of course, you can't forget about the base case, which is an empty list.
Replacing anything with anything in an empty list results again in an empty list. If the head of the list is the same as the element to replace, replace it, otherwise, keep it as it is. In both cases, process recursively the rest of the list. Translated from English into Prolog:
replace(_, _, [], []).
replace(O, R, [O|T], [R|T2]) :- replace(O, R, T, T2).
replace(O, R, [H|T], [H|T2]) :- H \= O, replace(O, R, T, T2).
All implementations presented so far in other answers are logically unsound when being used with non-ground terms. Consider the original query and a slight variant:
?- replace(3,three,[1,2,3],Xs).
Xs = [1,2,three] ; % OK: correct
false
?- A=3, replace(A,B,[1,2,3],Xs). % OK: correct
Xs = [1,2,B], A = 3 ;
false
It works! Let's ask some very similar queries:
?- replace(A,B,[1,2,3],Xs). % FAIL: should succeed more than once...
Xs = [B,2,3], A = 1 ; % ... but the other solutions are missing
false
?- replace(A,B,[1,2,3],Xs), A=3. % FAIL: this query _should_ succeed ...
false % ... it does not!
What's going on? Put the blame on meta-logical builtins (!)/0 and (\=)/2, which are very hard to use right and often make code brittle, impure, and logically unsound.
To preserve logical soundness, stick to logical purity and abstain from meta-logical "features" whenever possible! Luckily, most Prolog implementations support dif/2 as a logical alternative to (\=)/2. Let's use it:
% code by #svick, modified to use dif/2 instead of (\=)/2
replaceP(_, _, [], []).
replaceP(O, R, [O|T], [R|T2]) :- replaceP(O, R, T, T2).
replaceP(O, R, [H|T], [H|T2]) :- dif(H,O), replaceP(O, R, T, T2).
Let's run above queries again, this time with the improved replaceP/4:
?- replaceP(3,three,[1,2,3],Xs).
Xs = [1,2,three] ; % OK: correct, like before
false
?- replaceP(A,B,[1,2,3],Xs). % OK: four solutions, not just one
Xs = [B,2,3], A = 1 ;
Xs = [1,B,3], A = 2 ;
Xs = [1,2,B], A = 3 ;
Xs = [1,2,3], dif(A,1),dif(A,2),dif(A,3) ;
false
?- replaceP(A,B,[1,2,3],Xs), A=3. % OK (succeeds now)
Xs = [1,2,B], A = 3 ;
false
?- A=3, replaceP(A,B,[1,2,3],Xs). % OK (same as before)
Xs = [1,2,B], A = 3 ;
false
replace(_, _ , [], []).
replace(X, Y, [ X | Z ], [ Y | ZZ]):- ! , replace( X, Y, Z, ZZ).
replace(X, Y, [ W | Z], [ W | ZZ] :- replace(X, Y, Z, ZZ).
Though, one would usually arrange the 3. arg to be the first one. And strictly speaking above does not replace anything in the list, it just anwsers if 4th arg is like the one in the 3rd but with Y' instead of X'.
replace(E,S,[],[]).
replace(E,S,[E|T1],[S|T2]):-replace(E,S,T1,T2).
replace(E,S,[H|T1],[H|T2]):-E\=H, replace(E,S,T1,T2).
the idea is simple, if the elements match, change it, if not, go forward until empty.
domains
I=integer*
K=integer*
Z=integer
A=integer
predicates
nondeterm rep(I,Z,A,K)
clauses
rep([],_,_,[]).
rep([Z|T1],Z,A,[A|T2]):- rep(T1,Z,A,T2).
rep([H|T1],Z,A,[H|T2]) :- rep(T1,Z,A,T2).
goal
rep([1,2,3],2,4,X).