1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kinozato
  • 13
  • 1
  • 6
  • In addition to what @GuyCoder writes below, _Prolog variables must be capitalized_! – Daniel Lyons Nov 27 '18 at 18:22
  • You might find this [answer](https://stackoverflow.com/a/53322558/1243762) has more working code for what you are attempting. – Guy Coder Nov 27 '18 at 18:33
  • In Prolog, you don't "declare" variables. Prolog variables are not typed. They are just a variable that can be bound to a Prolog term (making it no longer variable). Numbers and lists are examples of Prolog terms. – lurker Nov 27 '18 at 19:20
  • Is there any reason you are not accepting my answer? i.e. click on the check mark next to the answer. – Guy Coder Dec 18 '18 at 23:10
  • @GuyCoder I keep pressing the checkmark but nothing happens! – Kinozato Dec 31 '18 at 16:27
  • @Wirito The check mark is active as I write this. Thanks. :) – Guy Coder Dec 31 '18 at 16:28
  • @GuyCoder How come I don't see it on my end? Usually, when I browse other questions, the best answer is clearly marked but here I don't see that message. Even if there's only your answer shouldn't it show that? – Kinozato Dec 31 '18 at 16:39
  • @Wirito Good question. Try refreshing your browser page, and if that doesn't work restart your browser. If that doesn't work then ask this question on the [meta](https://meta.stackoverflow.com/) site. – Guy Coder Dec 31 '18 at 16:44

1 Answers1

3

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).      
Guy Coder
  • 24,501
  • 8
  • 71
  • 136