4/20/2010

Lisp: the Ultimate Programming Language Giuseppe Attardi Università di Pisa

Quotes 

If it works, it is no longer AI (Artificial Intelligence) Marvin Minski



Lisp is executable XML with a friendlier syntax someone who rediscovered Lisp



I wonder why replacing parenthesis with angle

brackets should make a big difference Jim Miller

1

4/20/2010

Syntax, syntax 

People complained because of Lisp syntax



Once a Japanese programming, when he heard that I was a Lisp programmer, told me in admiration: ―Lisp is vely vely difficult: many many parentheses‖



Ironically, even worse syntax has become popular

Ant: XML madness 

Ant is a build system for Java



Ant takes an XML file with specific build

instructions and interprets them by running specialized Java code

2

4/20/2010

Ant Example clean" description="remove intermediate files">

Makefile clean: rm –fr classes clobber: clean rm hello.jar classes/%.class: %.java javac -d classes $
(setq c1 (Complex 1 2)) > (cl 'x) 1

> (cl 'y) 2 > (setq c2 (c1 'add c1)) > (c2 'y) 4

Closure Implementation 

Pair of:  environment (variable bindings)  function

x: y:

2 3

(lambda (msg &optional arg) …

12

4/20/2010

Meta Interpreter (defun eval (x env) (cond ((numberp x) x) ((symbolp x) (lookup x env)) ((eq (first x) ‘quote) x) (t (apply (first x) (evlis (rest x) env) env)) ) )

Environments 



A list of key-value pairs

((N 3) (X (red green blue)))

We say that:

 name N is bound to 3  name X is bound to the list (red green blue)  If name appears twice, the first is chosen:

(define lookup (x env) (cond ((null env) NIL) ((eq x (first (first env))) (second (first env))) (t (lookup x (rest env))))

13

4/20/2010

Apply (define apply (fn args env) (cond ((null fn) NIL) ((primitive fn) (primapply fn args env)) ; +, =, … ((symbolp fn) (apply (lookup fn env) args env)) ((eq 'lambda (first fun)) (eval (third fn) (bind (second fn) args env))) (t (apply (eval fn env) args env))) ) )

Functional Arguments (defun map (FUN X) (cond ((null X) X) (t (cons (FUN (first X)) (map FUN (rest X)))) ) ) applies function FUN to each element of the list X and returns a list of the results

(map '(lambda (x) (list x x)) '(CHITTY BANG)) returns ((CHITTY CHITTY) (BANG BANG))

14

4/20/2010

Funarg Problem (defun consall (X YS) (map '(lambda (y) (cons X y)) YS)) (consall 'BEAT '(HARVARD YALE)) we expect to get: ((BEAT HARVARD) (BEAT YALE)) we actually get: ((HARVARD YALE) HARVARD) ((YALE) YALE))

Why? Because (lambda (y) (cons X y))

is called within map, which binds X to the second argument: (defun map (FUN X) (cond ((null X) X) (t (cons (FUN (first X)) (map FUN (rest X)))))) which in the first call is (HARVARD YALE) and in the second is (YALE)

15

4/20/2010

Solution: closure Capture the environment with the FUNCTION operator: (defun consall (X YS) (map (function (lambda (y) (cons X y)) YS))) with: (consall ‘BEAT '(HARVARD YALE)) we actually get: ((BEAT HARVARD) (BEAT YALE))

Fixing eval (defun eval (x env) (cond ((numberp x) x) … ((eq (first x) 'function) (list 'closure (second x) env)) … ) Create closure

storing the current environment

16

4/20/2010

Fixing apply: (define apply (fn args env) (cond ((null fn) NIL) … ((eq 'closure (first fun)) (apply (second fn) args (third fn)) … ) )

Scheme 

Dialect of Lisp that adopted lexical scoping (from Algol 60) and turned all functions into

lexical closures, thereby avoiding to explicitly use FUNCTION 

Lexical scoping allows determining which free variables are used in a function and hence avoiding to store the whole environment

17

4/20/2010

Meta Programming 

Lisp macros are the most powerful



They provide access to the program code itself, represented as a list and can manipulate it with the full language power  (typical macro processors for instance do not allow recursion)

Macros: extending the language Function are not enough. Trying defining if: (defun if (test then else)

(cond (test then) (t else))) (if (= x 0) infinity (/ 1 x)) ;; Oops…

18

4/20/2010

Part of the issue on evaluation order



Call by value



Call by name



Call by need



Call by text

Defmacro (defmacro if (test then else) (list 'cond (list test then) (list t else))) A macro is dealt specially by eval: it applies the macro to the argument expressions, and then it evaluates the resulting expression. For example:

(if (= x 0) infinity (/ 1 x)) => (cond ((= x 0) infinity) (t (/ 1 x)))

19

4/20/2010

HTML Templates 

HTML templates or Web scripting languages adopt an opposite convention  everything is constant except when escaped





HTML

Hello {$X} PHP