Hacker News new | past | comments | ask | show | jobs | submit login

it might be just you and a bunch of other people who don't understand prolog.

if statements are not horn clauses. if statements are procedural and specify execution order and don't pattern match.

prolog's execution is resolution, done using unification over logic variables.

as a simple example: member/2

member(X,[X|_]). member(X,[_|T]) :- member(X,T).

if I query: member(1,[1,2,3]) this is true

if I query:

member(X,[1,2,3]), this is true, for X=1, X=2 and X=3

similarly you can ask append([1,2,3],[4,5,6],X) to get X=[1,2,3,4,5,6]. and ask append([1,2,3],X,[1,2,3,4]) to get X=4

for example:

to produce all possible combinatons of items from a series of lists:

comb([],[]) comb([H|T],[X|Y]) :- member(X,H), comb(T,Y).

so if I do

comb([[a,b,c],[d,e,f],[g,h,i]],X) I get X = a,d,g or X = a,d,h and so on and so on

as you might see, it is a little bit more obvious that it isn't just a series of 'if statements' but a series of relations between clauses




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: