Sunday, 15 February 2015

Scheme expand procedure calls -



Scheme expand procedure calls -

given function such tree-recursive implementation of fibonacci recurrence, how show each step in evaluation of look such (fib 5)?

(define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1) (fib (- n 2))))))

for example, output:

(fib 5) (+ (fib 4) (fib 3)) (+ (+ (fib 3) (fib 2)) (+ (fib 2) (fib 1))) (+ (+ (+ (+ (fib 1) 0) (fib 1) (+ (fib 1) 0)) (+ (+ (fib 1) 0) (fib 1))) (+ (+ (+ (+ 1 0) 1 (+ 1 0)) (+ (+ 1 0) 1))

i know using quasiquoting, can partially evaluate expression, in:

`(+ ,(* 3 4) (- 0.1 2)) ; evaluates -┐ (+ 12 (- 0.1 2)) ; <----┘

however, have not been able utilize show each step in evaluation. know modify scheme interpreter peter norvig's lis.py, way within language itself. how can this?

you mean, this?

(define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else `(+ ,(fib (- n 1)) ,(fib (- n 2))))))

for example:

(fib 5) => '(+ (+ (+ (+ 1 0) 1) (+ 1 0)) (+ (+ 1 0) 1))

of course, above homecoming final result of evaluation. using built-in stepper 1 in racket, can see each intermediate step. little how-to see answer, happens show fibonacci function, too.

scheme

No comments:

Post a Comment