Subscribed unsubscribe Subscribe Subscribe

SE Can't Code

A Tokyo based Software Engineer. Not System Engineer :(

Evaluation in Lisp.

In Lisp, There are two ways of evaluation below:

  1. Normal-order evaluation
  2. Applicative-order evaluation

Normal-order evaluation is a way which simplify evaluation after it expand all formula(include operator and operands). And in contrast, Applicative-order evaluation evaluate a each of argument in formula. Lisp use an Applicative-order evaluation because it enable you to avoid evaluation that is duplicated and Normal-order evaluation is complex when it out of the range that is procedure can be models.

Initial definition before it evaluate

(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (f a) (sum-of-squares (+ a 1) (* a 2)))

Normal-order evaluation

(f 5)
(sum-of-squares (+ 5 1) (* 5 2))
(+ (square (+ 5 1)) (square (* 5 2)))
(+ (* (+ 5 1) (+ 5 1) (* (* 5 2) (* 5 2))))
(+ (* 6 6) (* 10 10))
(+ 36 100)
136

Applicative-order evaluation

(f 5)
(sum-of-squares (+ a 1) (* a 2))
(sum-of-squares (+ 5 1) (* 5 2))
(+ (square 6) (square 10))
(+ (* 6 6) (* 10 10))
(+ 36 100)
136
Remove all ads