I am new to prolog programming and have been told in a tutorial to define a list of structures (in a script) so that I can query it as a database. However I find it impossible to define this list as a variable in a script. When I define a list such as
X=[a,b,c].
I just receive an error saying
No permission to modify static_procedure `(=)/2'
Does prolog not support defining variables such as this? I am using SWI-Prolog under linux.
This operator is used to append an element at the beginning of a list. The syntax of the pipe operator is as follows : [a | L] Here L is a list and a is a single element. For example: If, L = [b,c,d] Then, [a | L] will result in [a,b,c,d]
% add_tail(+List,+Element,-List) % Add the given element to the end of the list, without using the "append" predicate. add_tail([],X,[X]). add_tail([H|T],X,[H|L]):-add_tail(T,X,L).
An easy way to create a list of a given length consisting of the same element, or just empty lists in this case, is to use maplist2 : generate_board(Length, Board) :- length(Board, Length), maplist(=([]), Board). Save this answer.
Lists are a common data structure in computer programming. In Prolog, lists are represented as a tree consisting of structures that have no arguments, an empty list, or two arguments: a head and a tail. The head is a used to store a particular term in the list and the tail is recursively the rest of the list.
When you write
X = [a, b, c].
It's read as
=(X, [a, b, c]).
which is read as a definition of a fact concerning the =/2
predicate. A fact where any free variable would be equal to [a, b, c]
. That is, you redefine =/2
. That's obviously not what you intend!
You have to remember in Prolog that variables are scoped only locally, inside a predicate. What would work is:
main :-
X = [a, b, c],
% do stuff with X.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With