/************************************************************************* name: sformat_as_lisp.pl version: 0.5 description: Format lists as Lisp lists, usually for output to Lisp. author: Tom Breton *************************************************************************/ :- module(sformat_as_lisp,[sformat_as_lisp/2]). /*======================================================================== Format lists as lisp lists, recursively. ========================================================================*/ %%This works under SWI prolog and should work under Sicstus. It will %%probably NOT work under any strictly Edinburgh prolog, because it %%uses (Sisctus style) sformat, not (Edinburgh style) swritef. %%Handle heterogeneous list-structures. %Single elements except [] are simply formatted with ~w. sformat_as_lisp(S,Element):- (atomic(Element), \+ Element=[] ; var(Element)), sformat(S,"~w",[Element]),!. %Lists including [] are surrounded by "()" sformat_as_lisp(S,List):- is_list(List), sformat_list_innards(Innards,List), sformat(S,"(~s)~n",[Innards]),!. %%Handle the innards of a single list. %No elements left gives the empty string sformat_list_innards(S,[]):- S="",!. %Real elements are concatenated. sformat_list_innards(S,[Element|R]):- sformat_as_lisp(S1,Element), ( R=[], S=S1 ; %A cdr that isn't a list is printed in dot notation atom(R), sformat_as_lisp(S2,R), sformat(S, "~s . ~s", [S1,S2]) ; sformat_list_innards(S2,R), sformat(S, "~s ~s", [S1,S2]) ), !. % %Local Variables: %mode: prolog %End: % sformat_as_lisp.pl ends here