/* ----------------------------------------- just a base case show-predicate ----------------------------------------- */ %% this puts out EVERY 5 in the list base1([]). base1([H|R]) :- H=5, writeln('I found this: '+H), base1(R). base1([H|R]) :- not(H=5), base1(R). %% this only puts out the FIRST 5 in the list base2([5|_]) :- writeln('I found the first 5'). base2([H|R]) :- not(H=5), base2(R). /* ----------------------------------------- just a left/right recursion show-predicate ----------------------------------------- */ recurse([]). recurse([H|Rest]) :- writeln('right...\t H is '+H), recurse(Rest), writeln('left....\t H is '+H). /* ----------------------------------------- gauss(+X,-Y) | gauss(-X,+Y) | gauss(+X,+Y) Y is computed by adding up all numbers from 1 to X You can specify one of X or Y, or both ----------------------------------------- */ gauss(X,Y) :- number(X), gauss_with_X(X,Y). gauss(X,Y) :- number(Y), gauss_with_Y(X,Y). /* gauss_with_X(+X,-Y) */ gauss_with_X(X,_) :- X < 0, !, fail. gauss_with_X(0,0). gauss_with_X(X,Y) :- X1 is X - 1, gauss_with_X(X1,Y1), Y is Y1 + X. /* gauss_with_Y(-X,+Y) this pipes the problem to our special accumulator predicate */ gauss_with_Y(X,Y) :- gauss_with_Y_2(X,Y,0). /* gauss_with_Y_2(-X,+Y,+Z) Z is just an accumulator for X: we'll add it up from zero to the value that X should have. Then we unify it with X and pass X up the recursion tree */ gauss_with_Y_2(_,Y,_) :- Y < 0, !, fail. gauss_with_Y_2(X,0,X). gauss_with_Y_2(X,Y,Z) :- Z1 is Z + 1, Y1 is Y - Z1, gauss_with_Y_2(X,Y1,Z1).