Manipulate strings in prolog
I have a list of stirings I need to manipulate and write out. I get the strings the usual way with H|Tail recu开发者_如何学Pythonrsion. H will look something like "statement(foo, foo2, foo3, foo4, foo5)" I want to be able to write out only foo, foo2, foo3 on separate lines
out: foo
bar: foo2 ... ... div: foo5Convert string to codes, codes to a term, then destructure the term:
/* SWI Prolog
*/
read_from_string(String, Term) :-
string_to_list(String, List),
read_from_chars(List, Term).
demo:-
String="statement(foo, foo2, foo3,foo4,foo5)",
read_from_string(String, Term),
Term =.. [Fst,Snd,Thr|Rest],
write(functor:Fst),nl,
write(arg1:Snd),nl,
write(arg2:Thr),nl,
write(rest:Rest),nl.
Demo session:
?- demo.
functor:statement
arg1:foo
arg2:foo2
rest:[foo3,foo4,foo5]
true.
Choose the items to print according to their respective positions in the list that resulted from univ(=..). Here they were all printed.
精彩评论