Calling the schema extension procedure
Given a feature like a recursive implementation of fibonacci recursives, how can I show each step when evaluating an expression such as (fib 5)
?
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1)
(fib (- n 2))))))
For example, I would like to 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 that by using quasi-quoting you can partially evaluate the expression, as in:
`(+ ,(* 3 4) (- 0.1 2)) ; evaluates to -β
(+ 12 (- 0.1 2)) ; <----β
However, I was unable to use this to show every step in the evaluation. I know I can change the schema interpreter, for example Peter Norvig's lis.py , but I would like to make a way to do this within the language itself. How can i do this?
source to share
Do you mean like 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, the above will only return the final score. Using the built-in stepper, like in Racket, you can see every intermediate step. For a small way to see this answer , which also shows the fibonacci function.
source to share