Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining lists in prolog scripts

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.

like image 784
woozle Avatar asked Apr 10 '12 10:04

woozle


People also ask

What is list in Prolog explain with example?

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]

How do I add an element to a list in Prolog?

% 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).

How do you create an empty list in Prolog?

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.

What is the list represented as Prolog?

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.


1 Answers

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.
like image 80
m09 Avatar answered Oct 01 '22 06:10

m09