Hacker News new | ask | show | jobs
by octo_t 4824 days ago
Prolog is declarative programming take to the maximum (excluding things like Answer Set Programming/clingo etc).

In Prolog you ask questions. For example: subset([1,2],[2]).

then it goes away and says "yes". Or you want to know if any subsets exist: subset([1,2],B).

B = [] B = [1] B = [2]

This makes it really really nice for some surprising tasks (Windows NT used to ship with a prolog interpreter for setting up the network)

4 comments

I would disagree - in a sense, Prolog is less declarative than Haskell. For example, the order of "procedure calls" matters in Prolog, a sign of imperative programming. There is no such thing in Haskell (unless imperative behavior is being simulated with Monads).
If you're familiar with or interested in Prolog, I would definitely recommend checking out Mercury. The language home page was just migrated and they're having broken link issues, but here's a link: http://www.mercurylang.org/ Also, you can check out the wikipedia page for a quick summary: http://en.wikipedia.org/wiki/Mercury_programming_language

The language has a lot of functionality that Prolog doesn't have and (thanks to a strong typing system) performs much better. It just needs a bigger community to support it.

Ooh, I forgot all about prolog!

I worked through the 7 languages in 7 weeks book, and solving a sudoku with prolog blew my mind. I think the first "real" programming I did was a sudoku solver in Excel and VBScript (yeuch).

Prolog is really awesome (and impressive) for solvers where there is an "optimum" solution for what you want.

Its fairly trivial to write a checkers AI in prolog if you can define what you want + need:

Or to take his doubling example...

    double([], []).
    double(H | T, H2 | T2) :- H2 is H * 2, double(T, T2).
That's a lower-level version of map in a language with destructuring: Haskell would let you say

    double numbers = map (\x -> x * 2) numbers
or

    double [] = []
    double (x:xs) = x * 2 : double xs
or

    double = map (*2)
I don't think #2 is more declarative than #1, let alone #3. Then again, I don't think any of these versions is very declarative.

Declarative would be numpy:

    doubled = numbers * 2