In: Computer Science
Given the following Scheme definition:
(define x '(define (fac n) (if (= n 0) 1 (* n (fac (- n 1))))))
(This does not define the factorial function fac, but the variable x.)
Write Scheme expressions in terms of x that would have the effect of extracting the following expressions:
E.g., the expression (car x) would extract define.
Given below statements are equivalent.
Below are the scheme expressions and their corresponding output :
1)
(cadr x) ==> (fac n)
2)
(caddr (car (cdr (car (cddr x))))) ==> 0
3)
(cadr (caddr (car (cdddr (car (cddr x)))))) ==> (- n 1)
4)
(car (caddr (car (cdddr (car (cddr x)))))) ==> fac (second occurance)
5)
(cadr (cadr (caddr (car (cdddr (car (cddr x))))))) ==> n (last occurance)
Snapshot showing step by step extractions of scheme expressions: (Tick marked are required ones)