Dutch flag 2

Description

Dutch Flag with difference-lists.

Source: The Art of Prolog

Download

Program source code: dutch_flag_2.pl

Listing

/*  
   dutch(Xs,RedsWhitesBlues) :-
	RedsWhitesBlues is a list of elements of Xs ordered 
		by color: red, then white, then blue.
*/
	:- op(40,xfx,\).
 
	dutch(Xs,RedsWhitesBlues) :-
		distribute_dls(Xs,RedsWhitesBlues\WhitesBlues,
			WhitesBlues\Blues,Blues\[]).
 
/*	
    distribute_dls(Xs,Reds,Whites,Blues) :-
	Reds,Whites,Blues are difference-lists of red, white,
		and blue elements in Xs, respectively.
*/
 
	distribute_dls([red(X)|Xs],[red(X)|Reds]\Reds1,Whites,Blues) :-
		distribute_dls(Xs,Reds\Reds1,Whites,Blues).
	distribute_dls([white(X)|Xs],Reds,[white(X)|Whites]\Whites1,Blues) :-
		distribute_dls(Xs,Reds,Whites\Whites1,Blues).
	distribute_dls([blue(X)|Xs],Reds,Whites,[blue(X)|Blues]\Blues1) :-
		distribute_dls(Xs,Reds,Whites,Blues\Blues1).
	distribute_dls([],Reds\Reds,Whites\Whites,Blues\Blues).
 
%	Program 15.6: Dutch Flag with difference-lists.

Comments

pl/prolog/pllib/dutch_flag_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