Personally, I find that less legible (or readable) ...
I completely agree. The reason to built this in as an option was to explore the possibilities to write a Lisp with only
lambda and less than the 5 functions stated by McCarthy, which are: =, atom?, cond, cons, first, rest, quote (different uppercase names in the original texts).
This doesn't have any practical implications, just a nice brain-teaser exercise. There are no intentions to introduce an
implicit if for real, it leads to hard to detect error conditions.
If we allow the
implicit if and also use the newLISP 'args' function we can do with just: =, atom?, cons, quote and args, bringing it down to 5 functions from previously 7. 'args' can be used to define at least: list, first, rest. Instead of 'cond' we would use nested implicit 'if's.
To write an 'eval' function in newLISP (that would be writing Lisp in Lisp), we would also need: 'empty?' , 'and', 'not', 'assoc', 'append', 'list', 'pair'. But all these can be built using the previous 5. Perhaps even 'cons' could be expressed using the other 4 :). The function 'args' would play an important role here ... Or perhaps somebody come up with an entirely different algorithm ...
In case you have nothing better to do, think about it ;-), here is a start:
Code: Select all
(define (EMPTY? x)
(= x '()))
(define (OR x y)
((= nil x) ((= nil y) nil true) true))
(define (NOT x)
((= nil x) true nil))
(define (AND x y)
((NOT (= nil x)) ((NOT (= nil y)) true nil) nil))
(define (LIST) (args))
(define (FIRST) ((args 0) 0))
(define (REST) (1 (args 0)))
...
These and CONS (cons) are enough to define PAIR (make a list of pairs from two lists), APPEND and ASSOC. Having all those and QUOTE (quote) and ATOM? (atom?) you can define (EVAL x envList), where x is any s-expression (symbol or list) and envList is an association list describing you variable bindings. As there is no 'set', variable bindings must be described in an association list. For LABEL we simply use 'define' as in:
(define EVAL (lambda (x a) ...))
Here a little help from the master McCarthy himself:
http://www.newlisp.org/notthewholetruth.pdf
Translated to Common Lisp by Paul Graham:
http://lib.store.yahoo.net/lib/paulgraham/jmc.ps
Or for the mathematically inclined:
http://www-formal.stanford.edu/jmc/recursive.pdf