5 ms·
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
by coffeemug 18y ago
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!
- anatoly 18y agoAh, 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.
- Hexstream 18y agoYou mean if one keeps learning after SICP? Bad example, because SICP tells you how to implement exactly this functionality (among other things).
- coffeemug 18y agoIf 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.
- moe 18y agoWhat is this snippet supposed to tell me? Looks completely meaningless to me.
- Ezra 18y agoBasically 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.
- moe 18y agoAh thanks for clarifying. Now it makes sense. Are there any real world applications for this, for us mortal web developers?
- Ezra 18y agoIf 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.
- gommm 18y agoIn 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...
- oconnor0 18y agoIt 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.
- newt0311 18y agoAs 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/ 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 http://en.wikipedia.org/wiki/Warren_Abstract_Machine
- aguha 18y agoRelations, not functions. Prolog.