CS 330 Lecture 39 – Logic programming
Agenda
- atoms and variables
- facts and rules
- a path in a graph?
- Prolog’s DFS algorithm
- even or odd?
- member?
- sorted?
- notmember?
- hascycle?
Code
pdb.pl
lang(scheme). lang(c). static(c). staticlang(X) :- lang(X), static(X). /* Graph stuff */ edge(a, b). edge(b, e). edge(b, d). edge(c, d). path(X, Y) :- edge(X, Y). path(X, Y) :- edge(X, Z), path(Z, Y). /* Is even or odd? */ isodd(0) :- fail. isodd(1). isodd(N) :- N > 1, M is N - 1, iseven(M). iseven(0). iseven(1) :- fail. iseven(N) :- N > 0, M is N - 1, isodd(M). /* Member? */ /* I is only element in list. */ ismember(I, [I]). /* I is first element in list. */ ismember(I, [I|_]). /* I is in the tail. */ ismember(I, [_|T]) :- ismember(I, T). /* Sorted? */ sorted([]). sorted([_]). sorted([A,B|T]) :- A =< B, write(A), write(' is <= '), write(B), nl, sorted([B|T]).
Haiku
lang(scheme). lang(c). lang…
perfectlang(X) :- lang(X), fail.
perfectlang(Y). no
perfectlang(X) :- lang(X), fail.
perfectlang(Y). no