I read in the book Land of Lisp that the lambda function is the only built-in function. However I don't really understand how that is possible because I thought you would at least need one command for addition, one for comparing numbers, and one for moving data from one variable to another. I was wondering if someone could explain to me how lisp does it. I'm not a mathematician so if it is possible could you also explain it without a whole lot of complex math?
3 Answers
What 'Land of Lisp' is saying here is not that lambda is the only Lisp primitive, but rather that (according to Alonzo Church's lambda calculus, which Lisp has theoretical underpinnings) one could implement the rest of Lisp with lambda, as the lambda calculus is equivalent to a Universal Turing Machine.
For most practical applications, lambda is used to define anonymous functions.
1 Comment
That's a difference between theory and a real programming language.
Lisp took ideas from Lambda Calculus, but does not implement it. The lambda calculus describes a system to do calculation using functions. It is useful to understand Lambda Calculus, but you won't program in pure Lambda Calculus when you use Lisp.
As a programming language, Lisp has all kinds of data types and operations for those (numbers, strings, characters, cons cells, symbols, functions, ...).
Compare that to Turing Machines and something like the programming language C.
Comments
You're confusing some things here. lambda is not a function. It's a construct built into the Lisp language.
Any practical Lisp will have lots of built-in functions; it needs at least car and cdr to pick lists apart and some primitive arithmetic functions cannot be defined in terms of other functions.(*) Also, the "non-functional" parts of Lisp such as setf need some primitives.
[*] You can do Church arithmetic in Lisp, but then you can't pretty-print the results due to Lisp's type system but whether you can properly print the result depends on the Lisp variant.
4 Comments
car, cdr, and cons from pure lambda calculus - Lisp only provides them because they should be efficient. (defn cons [x y] (fn [f] (f x y))) (defn car [x] (x (fn [x y] x))) (defn cdr [x] (x (fn [x y] y))). This satisfies the only requirement of cons/car/cdr: that (eq (car (cons x y)) x) and (eq (cdr (cons x y)) y).
lambdais not a function, it's a special form. And there's certainly built-in functions in every lisp, that couldn't be defined by the user if they didn't exist (like for example the + function, as you mentioned). What I think the book might have said that lambda is the only way to define functions that is built into the language (e.g.defunis just a macro built on top of lambda).ifor something similar.if: you can use lambdas to delay evaluation. A lovely example of rebuilding a language with just lambdas is at experthuman.com/programming-with-nothing#booleans - basically definetrueas(lambda (x y) x)andfalseas(lambda (x y) y). Then you can write a non-eagerifas(lambda (test then else) (funcall (funcall test then else)))(or in Clojure,(fn [test then else] ((test then else)))).