Docsity

Prolog Examples: Min, Factorial, Length, Lisp Functions, Ancestors, Postfix, Parser, Study notes of Programming Languages

Toggle sidebar
bg1
PROLOG EXAMPLES
Script started on Thu 03 Mar 2005 12:23:23 AM CST
redstone% cat min.pro
min(X, Y, X) :- X <Y.
min(X, Y, Y) :- X >= Y.
redstone% sbp
SB-Prolog Version 3.1
?- consult(’min.pro’).
yes
?- min(0, 1, 0).
yes
?- min(17, 45, Minimum).
Minimum = 17
yes
?- min(65, 3, Minimum).
Minimum = 3
yes
?- ˆD
Halt. Program terminated normally
redstone% cat factorial.pro
factorial(0, 1).
factorial(N, Factorial) :-
M is N - 1, factorial(M, M_Factorial), Factorial is N * M_Factorial.
redstone% sbp
SB-Prolog Version 3.1
?- consult(’factorial.pro’).
yes
?- factorial(5, Factorial).
Factorial = 120
yes
?- factorial(10, Factorial).
Factorial = 3628800
yes
?- ˆD
Halt. Program terminated normally
pf3
pf4
pf5

Partial preview of the text

Download Prolog Examples: Min, Factorial, Length, Lisp Functions, Ancestors, Postfix, Parser and more Study notes Programming Languages in PDF only on Docsity!

PROLOG EXAMPLES

Script started on Thu 03 Mar 2005 12:23:23 AM CST

redstone% cat min.pro min(X, Y, X) :- X < Y. min(X, Y, Y) :- X >= Y.

redstone% sbp SB-Prolog Version 3.  ?- consult(’min.pro’). yes  ?- min(0, 1, 0).

yes  ?- min(17, 45, Minimum).

Minimum = 17 yes  ?- min(65, 3, Minimum).

Minimum = 3 yes  ?- ˆD Halt. Program terminated normally

redstone% cat factorial.pro factorial(0, 1). factorial(N, Factorial) :- M is N - 1, factorial(M, M_Factorial), Factorial is N * M_Factorial.

redstone% sbp SB-Prolog Version 3.  ?- consult(’factorial.pro’). yes  ?- factorial(5, Factorial).

Factorial = 120 yes  ?- factorial(10, Factorial).

Factorial = 3628800 yes  ?- ˆD Halt. Program terminated normally

redstone% cat length.pro length([], 0). length([X  Xs], Length) :- length(Xs, Xs_Length), Length is Xs_Length + 1.

redstone% sbp SB-Prolog Version 3.  ?- consult(’length.pro’). yes  ?- length([a, b, c, d], Length).

Length = 4 yes  ?- length([a, [b, c]], Length).

Length = 2 yes  ?- length([], Length).

Length = 0 yes  ?- length([a, [b, c], d, [e, f, [g, h]]], Length).

Length = 4 yes  ?- ˆD Halt. Program terminated normally

redstone% cat lisp_functions.pro car([X  Xs], X).

cdr([X  Xs], Xs).

cons(X, Xs, [X  Xs]).

append([], Ys, Ys). append([X  Xs], Ys, [X  Zs]) :- append(Xs, Ys, Zs).

redstone% sbp SB-Prolog Version 3.  ?- consult(’lisp_functions.pro’). yes  ?- cons(x, [], A).

A = [x] yes  ?- cons(y, [x], B).

B = [y,x] yes  ?- append([y, x], [z], D).

D = [y,x,z] yes  ?- append([y, x], [z], D), cdr(D, Cdr_D), car(Cdr_D, Cadr_D).

D = [y,x,z] Cdr_D = [x,z] Cadr_D = x yes

Child = jake;

Father = bill Mother = mary Child = shelley;

Father = bob Mother = lisa Child = mary;

Father = frank Mother = joan Child = bill; no  ?- sibling(Child1, Child2).

Child1 = jake Child2 = jake;

Child1 = jake Child2 = shelley;

Child1 = shelley Child2 = jake;

Child1 = shelley Child2 = shelley;

Child1 = mary Child2 = mary;

Child1 = bill Child2 = bill; no  ?- true_sibling(Child1, Child2).

Child1 = jake Child2 = shelley;

Child1 = shelley Child2 = jake; no  ?- ancestor(Ancestor, jake).

Ancestor = bill;

Ancestor = mary;

Ancestor = frank;

Ancestor = joan;

Ancestor = bob;

Ancestor = lisa; no  ?- ˆD

redstone% cat postfix.pro /* This program converts a postfix expression, represented as a list of operands and operators, into a syntax tree. */

postfix(Exp, Tree) :- postfix_stack(Exp, [], [Tree]).

postfix_stack([], Tree, Tree). postfix_stack([Operator  Rest_exp], [Top, Next_top  Rest_stack], Tree) :- operator(Operator), postfix_stack(Rest_exp, [tree(Operator, Next_top, Top)  Rest_stack], Tree). postfix_stack([Operand  Rest_exp], Stack, Tree) :- postfix_stack(Rest_exp, [Operand  Stack], Tree).

operator(+). operator(-). operator(*). operator(/).

redstone% sbp SB-Prolog Version 3.  ?- consult(’postfix.pro’). yes  ?- postfix([a,b,c,*,+],Tree).

Tree = tree(+,a,tree(*,b,c))

yes  ?- postfix([a,b,+,c,*,d,/,e,-],Tree).

Tree = tree(-,tree(/,tree(*,tree(+,a,b),c),d),e)

yes  ?- ˆD Halt. Program terminated normally

script done on Thu 03 Mar 2005 12:24:30 AM CST