sicp每日一题[1.41]

Exercise 1.41

Define a procedure double that takes a procedure of one argument as argument and returns a procedure that applies the original procedure twice. For example, if inc is a procedure that adds 1 to its argument, then (double inc) should be a procedure that adds 2. What value is returned by

(((double (double double)) inc) 5)


理解了可以把过程(procedure)作为返回值的概念之后,这道题就变得非常简单,double过程只需要将传进来的参数过程连续执行2次就行

(define (double f)
  (lambda (x) (f (f x))))

(define (inc a)
  (+ a 1))

(define (halve a)
  (/ a 2))


(((double (double double)) inc) 5)

(((double double) halve) 1024)

; 执行结果:
21
64
posted @ 2024-09-02 10:22  再思即可  阅读(2)  评论(0编辑  收藏  举报