xsari3x
xsari3x

Reputation: 452

How to read an array "list" & print it in prolog?

How to read an array "list" & print it in prolog ? I need to :- Prompt user to insert an array The user some how tells me that He's finished Then I print it

I just can't think of how to make this in a predicate.

Upvotes: 0

Views: 1558

Answers (1)

Alexander Serebrenik
Alexander Serebrenik

Reputation: 3577

Is this what you would like to have?

1 ?- p(X).

|: a.

|: b.

|: c.

|: d.

|: end.

Code:-

X = [a, b, c, d].

This is how one can implement this behaviour:

p(X) :- read(A), q(A,X-[]).

q(end,X-X) :- !.

q(A,[A|X]-Y) :- read(B), q(B,X-Y).

Upvotes: 1

Related Questions