Reading — step 1 of 5
Learn
In Prolog you declare facts — things known to be true:
:- initialization(main).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
main :-
parent(tom, X),
write(X), nl, fail.
main :- true.
When you query parent(tom, X), Prolog finds all bindings where the fact matches: X = bob, X = liz. The , fail forces backtracking to find every answer; the second main clause catches the final "no more answers" state.
Compound terms group data: point(3, 4), book(title('Foundation'), author('Asimov')). The same atom can describe a relation (when used as predicate) or a structure (when used as a term).
Operators are infix functions: X is 2 + 3 evaluates and binds X to 5. =:= is numerical equality. = is unification (very different — see next lesson).
Reading stdin: read_string(user_input, _, _, _, Line) (SWI-Prolog) or build your own reader. GNU Prolog uses read_atom/1 for an atom, or get_char/1 for a character.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…