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.

5
  • 7
    Apart from practice, you mean? Commented Aug 4, 2011 at 12:43
  • User RPL is by far my favorite flavor of LISP. It doesn't even have any parentheses! en.wikipedia.org/wiki/RPL_(programming_language) I miss using my HP 50g :-( Commented Aug 18, 2011 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. Commented Aug 27, 2011 at 21:43
  • What's wrong with the prefix notation? Almost all the languages are using it. Function calls are almost always prefix. Commented Aug 29, 2011 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? :) Commented Aug 29, 2011 at 15:41

6 Answers 6

7

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".

3
  • What would you do with ( = 1 2 ) and ( > 4 2 ) Commented Aug 4, 2011 at 19:00
  • 3
    You'd use rhetorical questions. :) Equal? (What?), "1 and 2! (Erm... No...) Commented Aug 9, 2011 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. Commented Aug 27, 2011 at 22:11
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.

1
  • 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. Commented Aug 27, 2011 at 21:47
6

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); 
1
  • 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). Commented Aug 4, 2011 at 15:50
5

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.

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'.

1

When it is indented and aligned, prefix gives you a diagram resembling logic gates (but of course for any kind of function)

(and (or x y) (not (and z w))) 

enter image description here

Also visualizable as a tree in this form:

and -+-- or --+--- x | | | `-- y | `- not -+--- and -+--- z | `-- w 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.