Flatten 2

Description

Flattening a list using a stack

Source: The Art of Prolog

Download

Program source code: flatten_2.pl

Listing

/*
   flatten(Xs,Ys) :- Ys is a list of the elements of Xs.
*/
     flatten(Xs,Ys) :- flatten(Xs,[],Ys).
 
     flatten([X|Xs],S,Ys) :- 
		list(X), flatten(X,[Xs|S],Ys).
     flatten([X|Xs],S,[X|Ys]) :-  X \== [], flatten(Xs,S,Ys).
     flatten([],[X|S],Ys) :- 
		flatten(X,S,Ys).
     flatten([],[],[]).
 
     list([X|Xs]).
 
% Program 9.1b   Flattening a list using a stack

Comments

pl/prolog/pllib/flatten_2.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0