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

2019-08-16 13:59发布

问题:

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.

回答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).


标签: prolog