0

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.

xsari3x
  • 442
  • 2
  • 12
  • 36

1 Answers1

1

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).
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alexander Serebrenik
  • 3,567
  • 2
  • 16
  • 32