10

I'm reading about LISP.

I understand how prefix notation works at a certain level, but I was wondering if there are any tricks to making it intuitive.

Vivian River
  • 2,397
  • 5
  • 21
  • 32
  • 6
    Apart from practice, you mean? –  Aug 04 '11 at 12:43
  • User RPL is by far my favorite flavor of LISP. It doesn't even have any parentheses! http://en.wikipedia.org/wiki/RPL_(programming_language) I miss using my HP 50g :-( – Robbie Aug 18 '11 at 18:20
  • At first it seemed unintuitive, but now I think it's wonderful, and I've mostly done reading. It is actually how we speak: *"The sum of..., the product of..."* However, it does require a different kind of reading. Now you simply apply the operator to all the arguments, instead of reading them in a line. – Mark C Aug 27 '11 at 21:43
  • What's wrong with the prefix notation? Almost all the languages are using it. Function calls are almost always prefix. – SK-logic Aug 29 '11 at 10:07
  • Not really; "intuitive" is intuitive because you've learned it in a form of mathematical conventions for >20+ years. Since only LISP and maybe a few other languages use this convention, and you'll still learn the other one much more, it will hardly ever become equally "intuitive". And we're talking only about simple expressions here (2+3 ... how would you like to try a "half a page" liner? :) – Rook Aug 29 '11 at 15:41

5 Answers5

7

Tricks? What for?

It doesn't feel intuitive for you yet because your mental parser isn't used to it. It'll become better if you just use it and read it over and over again.

Falcon
  • 19,248
  • 4
  • 78
  • 93
  • I haven't even read *that* much of it, but now I think it is wonderful. It seems that my brain is beginning to have an almost "tactile" sense about the parentheses. I think the feeling will only become more keen. – Mark C Aug 27 '11 at 21:47
6

Mentally reading it left to right as spoken language with the proper verbs can help. For example (+ 3 2) could be "add three and two". In the more general case, you can say "perform $operation on $operands". Applied to the same case: "Perform the add operation on three and two".

Joris Timmermans
  • 8,998
  • 2
  • 36
  • 60
  • What would you do with `( = 1 2 )` and `( > 4 2 )` – Vivian River Aug 04 '11 at 19:00
  • 3
    You'd use rhetorical questions. :) Equal? (What?), "1 and 2! (Erm... No...) – Arafangion Aug 09 '11 at 23:40
  • @RiceFlour (Hey, I grew up eating things made from rice flour--I have a gluten problem.) I would read "Is one equal to two?", or "Are the following equal?" The greater-than and less-than operators are not so natural in prefix form, but I wonder how often they are used in Lisp code. – Mark C Aug 27 '11 at 22:11
5

You could think about it as a kind of function call:

(operator operand1 operand2 ...)

There is nothing very special about it. If you overload operators in C++ (and many other languages that allow it) you often have to define this kind of function exactly that way:

MyClass operator+(MyClass const& x, MyClass const& y);
Dave Nay
  • 3,809
  • 2
  • 18
  • 25
thorsten müller
  • 12,058
  • 4
  • 49
  • 54
  • Yep, this is how I do it. If you step back and look at method calls in Java, C#, etc. they're really just prefix notation with the parentheses in a slightly different place: MyMethod(arg1, arg2). – nlawalker Aug 04 '11 at 15:50
4

Many languages use a mix of prefix, infix and even postfix.

Lisp just uses only prefix - by default. If sin(x) is intuitive from mathematics, then (sin x) is not far away. If move(dog,home) is a traditional procedure call, then in Lisp it is just (move dog home).

Lisp does not make any exception for mathematics and treats +, -, * and others like ordinary function calls.

Rainer Joswig
  • 2,190
  • 11
  • 17
2

When (eventually) "Everything is a function call" (or a special form, or a macro-expansion, both having the surface syntax of "function call") clicked, it felt pretty natural.

So, for (= 1 2) I read that as 'call the numeric equals comparison on 1 and 2'.

Vatine
  • 4,251
  • 21
  • 20