« Return to Thread: Merging Lists and Writing Delimited Records

Merging Lists and Writing Delimited Records

by Carroll, Michael L. :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.

All,

 

In case any of you might find these useful (I have), here are a couple of predicates that help merge lists of lists and produce delimited records (to be read by something like Excel).  They seem to work fine for my work, but there may be corner cases where things fall apart.  At any rate, have at it.

 

% The following little predicate will facilitate the merging of arbitrary number of lists:

 

% heads/3:  Given a list of lists LoL, produce a list consisting only of the heads of the lists in LoL.

%           As a by product, heads/3 also produces the list LoT of the remaining tails of lists in LoL. 

%             It returns LoH=[] and LoT=[], when LoL is empty.

 

heads(LoL,LoH,LoT) :- heads(LoL,[],LoH,[],LoT).

 

heads([],LoH,LoH,LoT,LoT) :- !.

heads([[H|Tail]|T],HAcc1,LoH,TAcc1,LoT) :-

                        append(HAcc1,[H],HAcc2),

                        append(TAcc1,[Tail],TAcc2),

                        heads(T,HAcc2,LoH,TAcc2,LoT).

 

%  writeDelimRecs/3:  Assume lists of atoms and write delimited records to a given stream.

%  Writes nothing if LoL contains an empty list.

 

writeDelimRecs(_,_,LoL) :- member([],LoL),!.

writeDelimRecs(Stream,Delim, LoL) :- heads(LoL,LoH,LoT),

                                                            concat_atom(LoH,Delim,Rec),

                                                            write(Stream,Rec), nl(Stream),

                                                            writeDelimRecs(Stream,Delim,LoT).

 

Regards,
Mike

Michael L. Carroll
Software Integrator
GPS SE&I Program
SAIC
310-416-8357
BB: 424-207-7221

 

 « Return to Thread: Merging Lists and Writing Delimited Records