Oct 29, 2003 ------------- - More on PROLOG - in Prolog, all functions are predictes - i.e., return true or false - have to "fake" returns of other types with a padded argument - Writing simple functions using rules Files used in this session isempty([]). ismember(X,[X|_]). ismember(X,[_|Z]) :- ismember(X,Z). isalist([]). isalist([_|_]). factorial(0,1). factorial(X,Y) :- X > 0, /* some outputs below didn't have this line */ Z is X-1, factorial(Z,M), Y is M*X. sum(0,0). sum(X,Y) :- X > 0, /* some outputs below didn't have this line */ Z is X-1, sum(Z,M), Y is M+X. /* function to append lists */ /* first two are the inputs, last is the appended list */ append([],X,X). append(X,[],X). append([X|Y],Z,[X|M]) :- append(Y,Z,M). /* function to find the last element of a list */ last(X,List) :- append(_,[X],List). /* function to check if X is a member of list List ; fill in the blanks yourself member(X,List) :- append( , , ). */ Transcript of session: ---------------------- ?- consult(functions). % functions compiled 0.00 sec, 520 bytes Yes ?- [functions]. % functions compiled 0.00 sec, 0 bytes Yes ?- isempty(X). X = [] ; No ?- isempty([]). Yes ?- isempty([jack, jill]). No ?- make. % /root/Languages/Prolog/functions.pl compiled 0.00 sec, 352 bytes Yes ?- ismember(2,[1,2,3]). Yes ?- ismember(nil,nil). No ?- ismember(5,[1,2,3]). No ?- ismember(X,[1,2,3,4]). X = 1 ; X = 2 ; X = 3 ; X = 4 ; No ?- ismember(1,X). X = [1|_G208] ; X = [_G207, 1|_G211] ; X = [_G207, _G210, 1|_G214] ; X = [_G207, _G210, _G213, 1|_G217] ; X = [_G207, _G210, _G213, _G216, 1|_G220] ; X = [_G207, _G210, _G213, _G216, _G219, 1|_G223] ; X = [_G207, _G210, _G213, _G216, _G219, _G222, 1|_G226] ; X = [_G207, _G210, _G213, _G216, _G219, _G222, _G225, 1|_G229] ; X = [_G207, _G210, _G213, _G216, _G219, _G222, _G225, _G228, 1|...] Action (h for help) ? abort % Execution Aborted ?- ismember([book],[[book],[tommy],[jimmy, rang, the, bell]]). Yes ?- make. % /root/Languages/Prolog/functions.pl compiled 0.00 sec, 232 bytes Yes ?- isalist([book,justin]). Yes ?- isalist(justin). No ?- isalist([[justin]]). Yes ?- make. % /root/Languages/Prolog/functions.pl compiled 0.00 sec, 8 bytes Yes ?- isalist(X). X = [] ; X = [_G208] ; No ?- isalist([justin,jimmy,johnny]). No ?- [factorial]. % factorial compiled 0.00 sec, 692 bytes Yes ?- factorial(1,X). X = 1 ; No ?- factorial(X,1). X = 0 ; X = 1 ; No ?- factorial(10,X). No ?- make. % /root/Languages/Prolog/functions.pl compiled 0.00 sec, -8 bytes % /root/Languages/Prolog/factorial.pl compiled 0.00 sec, 48 bytes Yes ?- factorial(5,X). X = 120 Yes ?- factorial(10,X). X = 3628800 Yes ?- factorial(X,3628800). ERROR: Arguments are not sufficiently instantiated ^ Exception: (9) _L167 is _G160-1 ? abort % Execution Aborted ?- factorial(5,X). X = 120 ; ERROR: Out of local stack ?- make. % /root/Languages/Prolog/factorial.pl compiled 0.00 sec, 40 bytes Yes ?- factorial(5,X). X = 120 ; No ?- make. % /root/Languages/Prolog/factorial.pl compiled 0.00 sec, 360 bytes Yes ?- sum(4,T). T = 10 ; No ?- sum(T,68493). ERROR: Arguments are not sufficiently instantiated ^ Exception: (8) _G157>0 ? abort % Execution Aborted ?- [lists]. % lists compiled 0.00 sec, 620 bytes Yes ?- append([1,2],[3,4],Z). Z = [1, 2, 3, 4] ; No ?- append(Z,[3,4],[1,2,3,4]). Z = [1, 2] ; No ?- append([1,2,X,5],[6,7],[1,2,4,5,6,7]). X = 4 ; No ?- append(X,Y,[3,4,5,6]). X = [] Y = [3, 4, 5, 6] ; X = [3, 4, 5, 6] Y = [] ; X = [3] Y = [4, 5, 6] ; X = [3, 4, 5, 6] Y = [] ; X = [3, 4] Y = [5, 6] ; X = [3, 4, 5, 6] Y = [] ; X = [3, 4, 5] Y = [6] ; X = [3, 4, 5, 6] Y = [] ; X = [3, 4, 5, 6] Y = [] ; X = [3, 4, 5, 6] Y = [] ; No ?- [lists]. % lists compiled 0.00 sec, 160 bytes Yes ?- last(X,[3,4,5]). X = 5 ; No ?- last(3,X). X = [3] Yes ?- last(3,X). X = [3] ; X = [_G219, 3] ; X = [_G219, _G225, 3] ; X = [_G219, _G225, _G231, 3] ; X = [_G219, _G225, _G231, _G237, 3] ; X = [_G219, _G225, _G231, _G237, _G243, 3] ; X = [_G219, _G225, _G231, _G237, _G243, _G249, 3] ; X = [_G219, _G225, _G231, _G237, _G243, _G249, _G255, 3] Yes