Check for one-element list in Prolog - list

I'm very new to Prolog (coming from C++) and am having trouble understanding using lists as parameters. In particular, I am wondering how to account for the case in which a list only has one element (the tail is "empty"). Why can't I have [L] as a parameter by itself? Why must a tail be explicitly included?
/** Trying to print out the head of the list */
writeList([L|_]) :- write(L). /** Works fine */
writeList([L]) :- write(L). /** Doesn't work. Will return false, but why? */
writeList([L]) :- write([L]). /** I thought I'd try this to see if it was a
problem with write(), but this returns false as well. */
The only way solution I came up with (without creating tests after the ":-") was:
writeList([L|[]]) :- write(L).
But it seems a bit messy to have to specify the tail is []. Is there any cleaner way to do this?
Sorry if this question is very silly--I've tried to find an answer to this (quite basic) question but I suspect there's a problem in my fundamental understanding here.

The notation [X|Y] refers to a list whose first element is X and whose tail is Y.
The underscore _ is an anonymous variable and means any term. That's not what you want in this case.
A one-element list have an empty tail, so Y is bound to an empty list [], then the notation is: [X|[]]
In a similar way, for a two-element list: [X,Y|[]]
Check this Prolog Lists tutorial for more on this topic.

Related

How to declare a list in Prolog?

I have browsed through various answers on SO about how to declare a list but I keep getting error messages. I am reading the section on lists from a book that I have but there still isn't an example on how to correctly declare them.
I am doing a project for my class. I have a random set of questions but when the user answers one then that question cannot be repeated (questions are to be random).
I have this part done but I wanted to create a list so that when a question is asked, I want to add that question number to my list. I have tried various ways and I still can't do it!
test(N):- list(P), member(N, P).
list = [].
start :-
write('Answer the questions correctly'), nl,
X is 0,
push(X,list,[X|list]),
test(X).
This snippet is just to make the list code. As I understand it I want to push X, in this case 0, to the head of the list. Since my list was declared as empty I figure it would work. I am getting this error:
No permission to modify static procedure `(=)/2'
I have tried to understand what this means but because everyone's code is different there are many different answers and I am overwhelmed. This is my first time programming in Prolog.
No permission to modify static procedure `(=)/2'
In Prolog you do not construct list by declaring them as you tried to do with
list = [].
Prolog values start with lower case letters and variables start with upper case letters. That is not common among programming languages but makes it easy to create new variables, you don't have to declare them, just use an upper case letter where you need a variable.
Prolog does not use assignment or have methods. Prolog uses syntactic unification and has predicates. So when you see [] as a argument being passed, that is when the list is either constructed, or unified with a variable.
You probably want something like this
begin :-
% In the next statement I am doing what you would consider
% constructing a list.
ask([]).
ask(List) :-
write('Answer the questions correctly'), nl,
get_answer(A),
% Here the answer in A is added to the head of the list using
% the list operator that combines a head with a tail, `|`.
% This is how your idea of a push is done with a list.
test([A|List]).
% When this is called from
% get_answer(A), A will be unified with 0.
get_answer(0).
% The next predicate `test` with two clauses does what your were trying to do with
% `member(N,P)`. It uses recursion which needs one clause to recursively process
% a list and one clause, the base case, to handle an empty list.
% When the list is empty, do nothing.
test([]).
test([H|T]) :-
% H is the head of the list
% do something with head of list by adding more code here.
% T is the tail of the list.
% Recursively call test with the tail of the list
% to process the remainder of the list.
test(T).

Prolog create list of lists

I'm having some (or a lot of) trouble with lists of lists in prolog.
So I have a list of numbers, say [5,6,1,3] as input.
The output should be [[5,25],[6,36],[1,1],[3,9]].
I already have a predicate that 'return's the 2 item lists (keep in mind that I'll have to change the get_squared_pair function to get some other relevant value):
get_squared_pair(Number, Result) :-
get_squared_value(Number, SquareValue),
Result = [Number, SquareValue].
get_squared_value(Number, Result) :-
Result is Number * Number.
Until here it's pretty logical. Now I need a predicate that recursively iterates though a list, adds the squared pair to a new list, and then returns this lists of lists. What I have right now is:
return_list([], 0).
return_list([Head | Tail], Result) :-
get_squared_pair(Head, Add),
append(Add,Result),
return_list(Tail, Result).
This doesn't work for a number of reasons, and it's mostly because I can't seem to figure out how the recursion actually works with lists, much less lists of lists. Also it's currently running in the wrong order which doesn't help.
I understand this might be a bit vague but I've tried googling my way out of this one but can't seem to translate what I find into my own problem very well.
Any help would be much appreciated!
Let's look at your get_squared_pair/2 first. Although it's working, it can be tidied up a bit which will also help understand how Prolog works. The primary mechanism of Prolog is unification, which is not the same as assignment which occurs in other languages. In unification, Prolog examines two terms and attempts to unify them by instantiating variables in one or both of the terms to make them match. There are some predicates in Prolog, like is/2 which are used to evaluate expressions in one argument, and then unify the first argument with that result.
Your first predicate, then, which you have written as:
get_squared_pair(Number, Result) :-
get_squared_value(Number, SquareValue),
Result = [Number, SquareValue].
get_squared_value(Number, Result) :-
Result is Number * Number.
Can be simplified in two ways. First, you can consolidate the get_squared_value/2 since it's just one line and doesn't really need its own predicate. And we'll rename the predicate so it's not imperative.
square_pair(Number, Square) :-
S is Number * Number, % Square the number
Square = [Number, S]. % Unify Square with the pair
Prolog can unify terms in the head of the clause, so you can avoid the redundant unification. So this is all you need:
square_pair(Number, [Number, Square]) :-
Square is Number * Number.
On to the main predicate, return_list/2. First, we'll rename this predicate to square_pairs. When doing recursion with lists, the most common pattern is to continue reducing a list until it is empty, and then a base case handles the empty list. Your implementation does this, but the base case is a little confused since the 2nd argument is an integer rather than a list:
square_pairs([], 0).
This really should be:
square_pairs([], []).
Your main predicate clause isn't making correct use of append/2. There are two forms of append in SWI Prolog: append/2 and append/3. You can look up what these do in the SWI Prolog online documentation. I can tell you that, in Prolog, you cannot change the value of a variable within a predicate clause once it's been instantiated except through backtracking. For example, look at the following sequence that might be in a predicate clause:
X = a, % Unify X with the atom 'a'
X = b, % Unify X with the atom 'b'
In this case, the second expression will always fail because X is already unified and cannot be unified again. However, if I have this:
foo(X), % Call foo, which unifies X with a value that makes 'foo' succeed
bar(X, Y), % Call bar, which might fail based upon the value of 'X'
In the above case, if bar(X, Y) fails, then Prolog will backtrack to the foo(X) call and seek another value of X which makes foo(X) succeed. If it finds one, then it will call bar(X, Y) again with the new value of X, and so on.
So append(Add, Result) does not append Add to Result yielding a new value for Result. In fact, append with two arguments says that the second list argument is the concatenation of all the elements of the first list, assuming the first argument is a list of lists, so the definition of append/2 doesn't match anyway.
When thinking about your recursion, realize that the argument lists are in one-to-one correspondence with each other. The head of the result list is the "square pair" for the head of the list in the first argument. Then, recursively, the tail of the 2nd argument is a list of the square pairs for the tail of the first argument. You just need to express that in Prolog. We can also use the technique I described above for unification within the head of the clause.
square_pairs([Head | Tail], [SqPair | SqTail]) :-
square_pair(Head, SqPair),
square_pairs(Tail, SqTail).
square_pairs([], []).
Now there's another simplification we can do, which is eliminate the square_pair/2 auxiliary predicate completely:
square_pairs([Head | Tail], [[Head, SqHead] | SqTail]) :-
SqHead is Head * Head,
square_pairs(Tail, SqTail).
square_pairs([], []).
There's a handy predicate in Prolog called maplist which can be used for defining a relationship which runs parallel between two lists, which is the scenario we have here. We can bring back the square_pair/2 predicate and use maplist:
square_pairs(Numbers, SquarePairs) :-
maplist(square_pair, Numbers, SquarePairs).
So you want to turn your list into another, such that each element (a number) is turned into a two-element list, the number and its square.
All you need to do is to tell that to Prolog. First, the second one:
turn_into_two(Num, [A,B]):-
what is A?
A is Num,
what is B? We just tell it to Prolog, too:
B is ... * ... .
Now, on to our list. A list [A|B] in Prolog consists of its head element A, and its tail B - unless it's an empty list [] of course. It doesn't matter what the list's elements are; a list is a list.
We need to account for all cases, or else we're not talking about lists but something else:
turn_list([], Res):-
so what is our result in case the list was empty? It should be empty as well, right?
Res = ... .
in the other case,
turn_list([A|B], Res):-
our result won't be empty, so it'll have its head and tail, correct?
Res = [C|D],
next we say what we know about the heads: the input list's head turns into that two elements list we've described above, right?
turn_into_two(A,C),
and then we say our piece about the tails. But what do we know about the tails? We know that one is the result of the conversion of the other, just as the whole list is:
turn_list( ... , ...) .
And that's it. Incidentally, what we've described, follows the paradigm of mapping. We could have used any other predicate in place of turn_into_two/2, and it would get called for each of the elements of the input list together with the corresponding element from the resulting list.

Prolog computing type of tuples

For an assignment, I have to create a type inference relation. here's the approach I used
tuples([]).
tuples(_|_).
type(tuples([]),tuples([])).
type(tuples(X|T),tuples(Y|Z)) :- type(tuples(T),tuples(Z)),type(X,Y).
I have already defined the type relation for all possible terms required for my assignment where y is the type of X in type(X,Y). For defining types of n-tuples, I used the approach similiar to the one used for appending lists.
But prolog always returns false when I ask
?-type(tuples([3,str]),Z)
or even
?-type(tuples([3,str]),tuples(Z))
or
?-type(tuples([3,str,4,abc,5,6]),Z)
i.e a list of length n, the answer returned is false.
Nothing changed even when I revered the sub-rules in the last rule.
tuples([]).
tuples(_|_).
type(tuples([]),tuples([])).
type(tuples(X|T),tuples(Y|Z)) :- type(X,Y),type(tuples(T),tuples(Z)).
I am not asking for alternative approaches to type of tuples to help me in my assignment but I can't figure out why this approach is not working.
It looks like your definition of a tuple is a List with length 2.
This rule does not check for that:
tuples(_|_).
What you probably want is this:
tuples([_,_]).
If you want it to check for any length list, use:
tuples([_|_]).
In the latter rule, the first wildcard represents the first item in the list (the head) and the second wildcard represents the rest of the list (the tail).

Prolog not showing output in correct format

I am new to Prolog and I am trying to remove the last element from a list. The code I have tried:
removeLast([],[_]).
removeLast([_|T], [_|OT]):-removeLast(T, OT).
Obtained from here. I executed the code from SWi Prolog and I am getting a weird output ...
1 ?-
| removeLast(X, [1,2,3,4]).
X = [_G299, _G302, _G305] .
This is supposed to show [1,2,3], instead it is showing some numbers(?)
I don't know what am I doing wrong, why it is displaying in this format? I tried every Google combination I know of to search this term, although I saw people use this format directly in their queries like, parent(X, _G3248).
Update: Thanks to #lurker, modifying the code to the original format gives the output correctly:
removeLast([],[_]).
removeLast([X|T], [X|OT]):-removeLast(T, OT).
15 ?- removeLast(X, [1,2,3,4]).
X = [1, 2, 3]
But can someone explain, what is _G3240?
Here's the difference in the behavior of the solutions. Let's take the malfunctioning one.
removeLast([],[_]).
This rule says that if I have a list of one element, then the corresponding list with last element removed is [] and I don't care what that one element was (it could even be a variable). This is true, and a valid rule.
removeLast([_|T], [_|OT]) :- removeLast(T, OT).
This rule says that [_|T] is [_|OT] with the last element removed if I don't care what their first elements are and T is OT with its last element removed (following the same rules). This doesn't sound quite right. It means that if I am removing the last element from a list, I don't care what element I have in my result. So you get an arbitrary list of elements whose count is one less than your original list. But the elements don't match those at the front of the original list. In Prolog, the two _ instances are different anonymous variables. They are not unified.
The corrected clause is:
removeLast([X|T], [X|OT]) :- removeLast(T, OT).
This says that a list [X|T] is the list [X|OT] with its last element removed if T is the list OT with its last element removed. The common X means they share the same head of the list, which is correct. When the recursion reaches the very last element of the second argument, then the first clause is matched and that single-element tail is replaced by the empty list []. And then you get the correct, final result.
I refer to the _ variable as "anonymous" rather than "don't care" since there are some uses for anonymous variables in which their values do matter. Often though, as in this case, they are used when the value is not being used.

How to split a list into a list of lists by removing a specific separation(Haskell)

I'm a newbie to Haskell, I have a problem. I need to write a function that splits a list into a list of lists everywhere a 'separation' appears.
I will try to help you develop the understanding of how to develop functions that work on lists via recursion. It is helpful to learn how to do it first in a 'low-level' way so you can understand better what's happening in the 'high-level' ways that are more common in real code.
First, you must think about the nature of the type of data that you want to work with. The list is in some sense the canonical example of a recursively-defined type in Haskell: a list is either the empty list [] or it is some list element a combined with a list via a : list. Those are the only two possibilities. We call the empty list the base case because it is the one that does not refer to itself in its definition. If there were no base case, recursion would never "bottom out" and would continue indefinitely!
The fact that there are two cases in the definition of a list means that you must consider two cases in the definition of a function that works with lists. The canonical way to consider multiple cases in Haskell is pattern matching. Haskell syntax provides a number of ways to do pattern matching, but I'll just use the basic case expression for now:
case xs of
[] -> ...
x:xs' -> ...
Those are the two cases one must consider for a list. The first matches the literal empty list constructor; the second matches the element-adding constructor : and also binds two variables, x and xs', to the first element in the list and the sublist containing the rest of the elements.
If your function was passed a list that matches the first case, then you know that either the initial list was empty or that you have completed the recursion on the list all the way past its last element. Either way, there is no more list to process; you are either finished (if your calls were tail-recursive) or you need to pass the basic element of your answer construction back to the function that called this one (by returning it). In the case that your answer will be a list, the basic element will usually be the empty list again [].
If your function was passed a list that matches the second case, then you know that it was passed a non-empty list, and furthermore you have a couple of new variables bound to useful values. Based on these variables, you need to decide two things:
How do I do one step of my algorithm on that one element, assuming I have the correct answer from performing it on the rest of the list?
How do I combine the results of that one step with the results of performing it on the rest of the list?
Once you've figured the answers to those questions, you need to construct an expression that combines them; getting the answer for the rest of the list is just a matter of invoking the recursive call on the rest of the list, and then you need to perform the step for the first element and the combining.
Here's a simple example that finds the length of a list
listLength :: [a] -> Int
listLength as =
case as of
[] -> 0 -- The empty list has a length of 0
a:as' -> 1 + listlength as' -- If not empty, the length is one more than the
-- length of the rest of the list
Here's another example that removes matching elements from a list
listFilter :: Int -> [Int] -> Int
listFilter x ns =
case ns of
[] -> [] -- base element to build the answer on
n:ns' -> if n == x
then listFilter x ns' -- don't include n in the result list
else n : (listFilter x ns') -- include n in the result list
Now, the question you asked is a little bit more difficult, as it involves a secondary 'list matching' recursion to identify the separator within the basic recursion on the list. It is sometimes helpful to add extra parameters to your recursive function in order to hold extra information about where you are at in the problem. It's also possible to pattern match on two parameters at the same time by putting them in a tuple:
case (xs, ys) of
([] , [] ) -> ...
(x:xs', [] ) -> ...
([] , y:ys') -> ...
(x:xs', y:ys') -> ...
Hopefully these hints will help you to make some progress on your problem!
Let's see if the problem can be reduced in a obvious way.
Suppose splitList is called with xs to split and ys as the separator. If xs is empty, the problem is the smallest, so what's the answer to that problem? It is important to have the right answer here, because the inductive solution depends on this decision. But we can make this decision later.
Ok, so for problem to be reducable, the list xs is not empty. So, it has at least a head element h and the smaller problem t, the tail of the list: you can match xs#(h:t). How to obtain the solution to the smaller problem? Well, splitList can solve that problem by the definition of the function. So now the trick is to figure out how to build the solution for bigger problem (h:t), when we know the solution to the smaller problem zs=splitList t ys. Here we know that zs is the list of lists, [[a]], and because t may have been the smallest problem, zs may well be the solution to the smallest problem. So, whatever you do with zs, it must be valid even for the solution to the smallest problem.
splitList [] ys = ... -- some constant is the solution to the smallest problem
splitList xs#(h:t) ys = let zs = splitList t ys
in ... -- build a solution to (h:t) from solution to t
I don't know how to test it. Anybody tells me how to write a function to a .hs file and use winGHCi to run this function?
WinGHCi automatically associates with .hs files so just double-click on the file and ghci should start up. After making some changes to the file using your favourite editor you can write use the :r command in ghci to reload the file.
To test the program after fixing typos, type-errors, and ensuring correct indentation, try calling functions you have defined with different inputs (or use QuickCheck). Note Maybe is defined as Just x or Nothing. You can use fromMaybe to extract x (and provide default value for the Nothing case).
Also try to make sure that pattern matching is exhaustive.