
How to Read Lisp Code - tallgiraffe
https://yoo2080.wordpress.com/2014/07/04/it-is-not-hard-to-read-lisp-code/
======
kirillzubovsky
For me it was always confusing why Lisp uses this notation (+ 1 2) until I
read this blog post, and now I get it. Lisp is functional, and it's using a
functional notation for everything. "The sum of one and two" makes total sense
now! Thanks.

~~~
kazinator
This is not because Lisp is functional. Most dialects of Lisp are not
functional languages. Many languages that are functional support 1 + 2 syntax
for addition.

Lisp uses a nested list structure for representing code, which encodes into a
parenthesized printed notation. We can think of (+ 1 2) as being an abstract
syntax tree node with the operator + and two child nodes 1 and 2.

Because addition is a function (it operates on values and produces values), it
is modeled as a function. The symbol + is simply the name of a library
function and thus (+ 1 2) denotes a call to that function.

Not everything in the operator position is a function! Often, a symbol in the
operator position is an operator.

The (symbol arg1 arg2 ...) notation isn't primarily functional; it is
primarily denotational. It denotes a syntax in which the symbol is the main
vocabulary item that gives it the meaning, and the arguments are modifiers.

It can represent ideas other than arithmetic. For instance, it could encode
facts in a logical system:

    
    
      (married john amy) ;; declaration of fact: john and amy are married
    

Or syntax:

    
    
      (bit man dog) ;; verb subject object
    

In Lisp code there are lots of operators that are not functions.

    
    
      (let ((x 3) (y 4)) (+ x 3))
    

_let_ isn't a function; if it were then ((x 3) (y 4)) would have to be an
argument expression that is evaluated, which is completely different from what
it's actually expressing.

That's basically it; this nested list is a notation for encoding ideas:
whatever ideas you want. In the notation, usually the first item is a symbol
which holds an important vocabulary item that is the key to the meaning.

Forms can alter the vocabulary that is active in whatever they enclose, and so
Lisp has to be read outside in.

In the above _let_ we know that because we are inside a _let_ , then (x 3)
doesn't mean call the function _x_ with argument 3. _let_ has altered the
vocabulary; it has its own rules and we must understand its interior according
to those rules.

~~~
kirillzubovsky
Thank you for the explanation, very interesting, and makes me want to dive
more into lisp and see what's possible with this approach. Next thing you
know, I am Clojure fanboy.

Can you help me understand, what is the purpose of (+ x 3) at the end of that
let statement. Is that just the default return from the original function,
using input given by the let function? Thanks!

~~~
kazinator
Again, if we say "let function" that is not accurate. _let_ is an _operator_
that creates a new nesting of the lexical scope in which it binds some
variables, and then evaluates forms in that environment.

 _let_ implements something similar to what is achieved with a combination of
special syntax in some other languages.

    
    
        Lisp                                 C
    
        (let ((x 1)                          { int x = 1;
              (y 2)                            int y = 2;
          (foo x y))                           foo(x, y); }
    

Both of these mean "create fresh instances of variables x and y, which are
scoped only to this construct, initializing them with 1 and 2, and then call
_foo_ with x and y arguments."

~~~
kirillzubovsky
Got it! Thanks.

