News for hackers. Stuff that matters.
Re:Not Lisp again.... by coffeemug on Thursday March 05, 2009 @05:15PM (#504834)
You know, if you keep learning you will never cease to have this feeling.

  ?- append([1, 2, 3], [4, 5, 6], X).
  X = [1, 2, 3, 4, 5, 6].

  ?- append([1, 2, 3], X, [1, 2, 3, 4, 5, 6]).
  X = [4, 5, 6].

  ?- append(X, [4, 5, 6], [1, 2, 3, 4, 5, 6]).
  X = [1, 2, 3].
And the definition of append:

  append([], X, X).
  append([X | Y], Z, [X | O]) :- append(Y, Z, O).
Magic!
Re:Not Lisp again.... by anatoly on Thursday March 05, 2009 @06:23PM (#504917)
Ah, thank you for rekindling that memory.

Prolog, where you just type what you want to achieve, and it goes ahead and does it for you. I really couldn't wrap my head around it for some time. I read up on backtracking and chased around derivations on paper to satisfy myself that something like append really worked.

Re:Not Lisp again.... by moe on Thursday March 05, 2009 @05:57PM (#504887)
What is this snippet supposed to tell me? Looks completely meaningless to me.
Re:Not Lisp again.... by Ezra on Thursday March 05, 2009 @06:23PM (#504918)
Basically what's going on is this, but with lists:

3 + 5 = X?

X = 8

3 + X = 8?

X = 5

5 + X = 8?

X = 3

In most programming languages, you can't infer a parameter by specifying a result.

(Though that's not strictly what's going on here, either)

You could further say something like X + Y = 8 and get

X = 0, Y = 8

X = 1, Y = 7

etc. for some definitions.

Which is, as the OP was getting at, sometimes incredibly useful.

Re:Not Lisp again.... by moe on Thursday March 05, 2009 @06:25PM (#504921)
Ah thanks for clarifying. Now it makes sense. Are there any real world applications for this, for us mortal web developers?
Re:Not Lisp again.... by Ezra on Thursday March 05, 2009 @06:40PM (#504941)
If you're asking "are there potentially awesome applications of this?" then the answer is probably "yes".

If you're asking "is there anything in the wild that I would know of and uses this?" then the answer is probably "no".

Most of the prolog projects I've ever seen are pretty inbred; implementing compilers or interpreters, hooks for another language-type things. Though it must be said that I'm not deeply embedded in the prolog community.

Re:Not Lisp again.... by gommm on Thursday March 05, 2009 @07:22PM (#504983)
In my 4th year university project we worked on an expert system for a card game in Prolog... For doing an expert system based AI, prolog is pretty good...
Re:Not Lisp again.... by oconnor0 on Thursday March 05, 2009 @06:11PM (#504904)
It tells you that Prolog's list append function has far more general applicability than just appending lists. Because of the way Prolog is designed you can use that function definition to do destructuring pattern matches on lists - to find differences, etc.

It means the tools you write for one use can be used completely differently than you intended, and in ways that continue to give you those "whoa!" moments.

Re:Not Lisp again.... by newt0311 on Thursday March 05, 2009 @06:26PM (#504922)
As has already been mentioned, thats not a function, it is a functor. It indicates a relation between two objects. Specifically, it tells the prolog runtime that a list appended to an empty list is just the list itself and that the resultant list and the first list argument should have the same prefix. Another thing to note is that the append function piggy-backs on the prolog version of list-appended cons-cells so that append function example is a bit deceiving. However, the power of prolog should not overlooked. For example, a call to append(X, Y, [1, 2, 3]) would have returned all possible values of X and Y which append to [1, 2, 3]. Something much harder in other languages.

An extremely (imho) good source for information on logic programs and how they are implemented is here: http://www.ida.liu.se/~ulfni/lpp/

A more practical guide to actually compiling these logic programs efficiently (or as efficient as they can be) is documented in Modern Compiler Design by Dick Grune et al. They actually describe the workings of a somewhat simplified workings of a Warren Abstract Machine (WAM) which can (to my knowledge) now approach to within an order of magnitude of standard programming languages. Pretty amazing when the generality of logic programming is considered. Details on the WAM here: http://en.wikipedia.org/wiki/Warren_Abstract_Machine

Re:Not Lisp again.... by aguha on Thursday March 05, 2009 @05:59PM (#504889)
Relations, not functions. Prolog.
Re:Not Lisp again.... by Hexstream on Thursday March 05, 2009 @06:23PM (#504919)
You mean if one keeps learning after SICP? Bad example, because SICP tells you how to implement exactly this functionality (among other things).
Re:Not Lisp again.... by coffeemug on Thursday March 05, 2009 @06:50PM (#504949)
If one keeps learning, dreaming, and building in general. I am just saying that no matter how many amazing things you've discovered, there's always more in store. Just never stop looking.