开发者

Prolog List of Constants to String

开发者 https://www.devze.com 2023-03-23 16:43 出处:网络
i have a list in input: [asd,qweqwe,fsdf,lkasd] As un can see from the code i want connect each constant of the l开发者_如何学Pythonist to output a single variable list.

i have a list in input: [asd,qweqwe,fsdf,lkasd] As un can see from the code i want connect each constant of the l开发者_如何学Pythonist to output a single variable list. i m using yap prolog, i consult this code and i write :- run. the write function print out _G1233 and not 'asd,asd2,asd3,asd4' why ? how i have to change the code for output me 'asd,asd2,asd3,asd4' ?

run :- toAtomicVars([asd,asd2,asd3,asd4],',',Out),
write(Out),nl.


toAtomicVars([],In,Out).
toAtomicVars([A|B],In,Out) :-
atomic_concat(A,In,Out1),
atomic_concat(',',Out1,Out2),
toAtomicVars(B,Out2,Out2).


you (should) get a warning like

Singleton variables: [In,Out]
Singleton variables: [Out]

that means that you dont really do anything with those variables:

toAtomicVars([],In,Out).

In and Out can be anything, so prolog just prints a dummy value, _Gsomething that means that the variable is not instantiated.

same thing happens in the second clause:

toAtomicVars([A|B],In,Out) :-
  atomic_concat(A,In,Out1),
  atomic_concat(',',Out1,Out2),
  toAtomicVars(B,Out2,Out2).

you dont say anywhere what is that Out.

personally i think that it would be easier if you just printed each variable recursively and then print a comma, something like:

print_list([X]):-
  write(X).
print_list([H|T]):-
   write(H),
   write(', '),
   print_list(T).

but if you want to put commas in the list you should add a rule that defines Out.

0

精彩评论

暂无评论...
验证码 换一张
取 消