简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM