2 ms·
Couple of weird things about Prolog: It's a pattern-matcher, in much the same way Haskell is -- but Prolog invented it first. Thus the first rule tries to match
by dreamcompiler 10y ago
Couple of weird things about Prolog: It's a pattern-matcher, in much the same way Haskell is -- but Prolog invented it first. Thus the first rule tries to match the args you give it, and if you give it the literals [] and 0, it just says "True" or "Yes" (depending on which Prolog you're using). If you give it something it cannot match to [] and 0, it will try the second clause and assume the first arg is a list with a head and a tail, and the second arg is an integer.
The other weird thing is that Prolog functions can often run backwards. If you ask Prolog
?- list_length(Foo, 4)
it will hand you back a list of 4 elements that it just made up on the spot. This is why Prolog rules are considered generalizations of functions: They don't have explicit inputs vs. outputs. They're relations, not functions.
This stuff blew my mind when I first encountered Prolog and it changed forever how I thought about programming.
- doublec 10y agoThe reversibility of Prolog computations was a big eye opener for me too back when I first encountered it. I recently wrote a post about relational programming using Prolog and Mozart/Oz as examples https://bluishcoder.co.nz/2017/03/20/relational-programming-in-mozart-oz.html https://bluishcoder.co.nz/2017/03/20/relational-programming-... Another interesting language in the same domain is Picat http://picat-lang.org/ http://picat-lang.org/