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

It's really quite different, and I don't think I can fully do it justice in a comment. I will give my favourite example though: it certainly helped me get the "aha" moment.

Let's say you have a collection of cities, and a collection of roads between them. And you want to answer the question: "is there a route from City A to City E?"

A solution in prolog would look something like this:

    /* First, the roads */

    road("A", "B").
    road("B", "C").
    road("C", "D").
    road ("D","E").

    /* Now the algoritm */

    route(From, To) :-
    road(From, To).

    route(From, To) :-
    road(From,Intermediate), route(Intermediate,To).

That's it. There's no need to loop/iterate/check conditions. Simply state the facts, ask the question, and the prolog interpreter will search for an answer:

    ?- route("A", "E").

    yes.



Why do you need route?

Can't you just use the roads you've got as the base case and then say

road(X, Z) :- road(X,Y), road(Y,Z)


> Why do you need route?

Because it creates a left-recursive call that will eventually cause a stack overflow. If you want to try it out, there's Swish[0] - an online prolog editor and interpreter.

[0]: https://swish.swi-prolog.org/




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: