Prolog find element in list of lists with preference - list

I have list of lists like:
L = [[Q,w,E,],[Q,w,Z,r],[A,s,D,f]]
I know the first two and I need to get the rest.
For example I have Q,w and I need to get Z,r or E,r.
I would like to somehow tell that with priority I always want that touple contain Z, but if doesnt exist give me E,r.
I tried:
member([Q,w,Z,VB],[[Q,w,E,o],[Q,w,Z,r],[A,s,D,f]]).
But that always give me Z = E, VB = o

First you need to know the difference in Prolog between an Atom and a Variable, you can read about their syntax here
Now, if you want a list of atoms that begin with an upper-case letter, you must enclose them in single quotes, otherwise prolog will interpret them as variables.
Now if you fix the syntax of your consult, you will get the following result:
?- member(['Q',w,Z,VB],[['Q',w,'E',o],['Q',w,'Z',r],['A',s,'D',f]]).
VB = o,
Z = 'E'
VB = r,
Z = 'Z'
false
Note how in this case I enclosed in single quote all atoms beginning with an upper-case letter, except for Z and VB in the first argument of the member/2 predicate, cause in this case they function as variables to be instanciated by prolog with the atoms needed to complete this case.

Related

How to check if a certain pattern exists in a list in prolog

i was trying to implement a prolog predict that checks if a certain pattern say (x,m) exists in a list or m exists at the very end of the list and count the number of its occurrence i never get an answer to the number of times the pattern existed.why?
my attempt was :
certainP([_,m],RESULT,W):-
W is RESULT+1.
certainP([x,m|T],START,RESULT):-
RESULT is START+1,
START is RESULT,
certainP(T,START,RESULT).
This should do what you describe:
certainP([],N,N).
certainP([_],N,N).
certainP([x,m|T],N,N2) :-
N1 is N+1,
certainP(T,N1,N2).
certainP([_|T],N,N2) :-
certainP(T,N,N2).
This assumes that the middle argument is provided an initial numeric value in the query.

Prolog assigning "\r" when 13 is used

Started learning prolog this past week and am having trouble with how variables are assigned in this particular case where it is a list containing one element inside of a list.
?- X = [[13]].
X = ["\r"]
Why is prolog assigning ["\r"]? What can be done so it assigns that actual value?
It is exactly that "actual value". It just shows up differently on the top level.
In most Prologs, a list of small integers and a list of character codes are identical. In other words, those two, [13] and "\r", cannot be told apart. So, when you put a 13 in a list, your particular Prolog thinks you meant to make a list of (ASCII) codes, and since 13 is the value of the carriage return, this is what it shows you. (Here, a string of characters enclosed in double quotes is just another way of representing that list of small integers). But this really depends on the implementation. Here is what my Prolog (SWI-Prolog v7) tells me:
?- X = [[13]].
X = [[13]].
Just to confuse you even more, you could have written the above in another way: as a list of character codes in 0' notation:
?- X = [0'\r], Y = [0'\xd].
X = Y, Y = [13].
(Not sure what you are going to see: consult the documentation of you implementation for such details.)
The 0' notation is a way to directly take the numerical value of a character: 0'a is the same as 97. So, the X above is the numerical value of the carriage return, which is 13, which is 0xD in hexadecimal.

Can someone please explain to me how the pipe "|" character works in Prolog?

I have this code:
[a,b,[]]=[First,Second,Third|Fourth].
and it gives me the following output:
First = a, Second = b, Third = Fourth, Fourth = [].
I'd like to know how Third got assigned to Fourth.
The pipe character is very similar to the "consing dot" in Lisp. The variable after the pipe takes the entire remainder of the corresponding list.
So, here we expect Third to bind to the explicitly given empty list in the data. But there is nothing after that and so Fourth also binds empty.
Third being bound to Fourth is just an indirect way of Third being bound to empty.
See my answer here: https://stackoverflow.com/a/7559044/467473 for details on how Prolog lists are implemented.
Basically, a prolog list is a simple data structure. The empty list is denoted by the atom []. A non-empty list is the structure ./2. The left argument in this structure is the head of the list; the right argument is the tail of the list, which is another list (either the empty list [] or a non-empty list (./2).
The friendly list notation is just syntactic sugar on top of this. The expression [H|T] is exactly the same as the expression .(H,T). The expression [a,b|T] is exactly the same as .(a,.(b,T)). And the expression [a,b,c] is exactly the same as .(a,.(b,.(c,[]))).

write elements in a list in prolog

I am learning prolog. How do you write elements in a list in prolog where the list may contains elements beginning with a capital letter.
for example: I have the predicate my_write/1
my_write([]). /* Base case: An empty list */
my_write([X|R]):- write(X),nl,my_write(R). /* Recursive case: */
But when I run my_write([How, are, you]). I get [_G749,are,you]
I know that words that begin with capital letter are variables in prolog.
I know you could enclose the word in a list that begins with capital letter in quotes, but is it possible to do it without having to do that.
I don't think there is a (better) way to do it instead of my_write(['How', are, you]).
BTW, you need to quote not only terms starting with a capital letter, but also terms starting with an underscore sign, or terms with spaces in their names.
SWI-Prolog has code_type/2 to handle character duties, with a not very user friendly syntax.
If you need to make upper case the first letter of an atom:
upcase_first_char(Plain, Proper) :-
atom_codes(Plain, [First|Cs]),
code_type(First, to_lower(Upcase)),
atom_codes(Proper, [Upcase|Cs]).
yields
?- upcase_first_char(carlo, V).
V = 'Carlo'.
to be used, in your case, like
my_write([First|Rest]) :-
upcase_first_char(First, Upper),
maplist(writeln, [Upper|Rest]).
yields
?- my_write([how, are, you]).
How
are
you

How to print comma-separated list with hamlet?

With the hamlet templating language that comes with yesod, what is the best way of printing a comma-separated list?
E.g. assume this code which just prints one entry after another, how do I insert commas in between the elements? Or maybe even add an “and” before the last entry:
The values in the list are
$ forall entry <- list
#{entry}
and that is it.
Some templating languages such as Template Toolkit provide directives to detect the first or last iteration.
I don't think there's anything built-in like that. Fortunately, it's easy to use helper functions in Hamlet. For example, if your items are plain strings, you can just use Data.List.intercalate to add commas between them.
The values in the list are
#{intercalate ", " list}
and that is it.
If you want to do fancier things, you can write functions to work with Hamlet values. For example, here's a function which adds commas and "and" between the Hamlet values in a list.
commaify [x] = x
commaify [x, y] = [hamlet|^{x} and ^{y}|]
commaify (x:xs) = [hamlet|^{x}, ^{commaify xs}|]
This uses ^{...} syntax to insert one Hamlet value into another. Now, we can use this to write a comma-separated list of underlined words.
The values in the list are
^{commaify (map underline list)}
and that is it.
Here, underline is just a small helper function to produce something more interesting than plain text.
underline word = [hamlet|<u>#{word}|]
When rendered, this gives the following result.
The values in the list are <u>foo</u>, <u>bar</u> and <u>baz</u> and that is it.