(define (power-n x)The above definition basically defines a generator function. A slightly different definition using a named let is
(define power-x
(lambda (n)
(if (= n 0)
(list 1)
(cons (power x n) (power-x (- n 1))))))
power-x)
(define (power-n x)In the first case, we define power-x and then explicitly return it. In the second version, we create an anonymous function and return that automatically.
(lambda (n)
(let power-x ((n n))
(if (= n 0)
(list 1)
(cons (power x n) (power-x (- n 1)))))))
In both cases, we can create generators such as power-two as
(define (power-two n)Thanks to a thread on comp.lang.scheme for fixing things!
((power-n 2) n))
Myopic view of scheme
No comments:
Post a Comment