SICP 练习 2.27
题目
Modify your
reverseprocedure of exercise 2.18 to produce adeep-reverseprocedure that taks a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well.
(define x (list (list 1 2) (list 3 4)))
;; x - ((1 2) (3 4))
(deep-reverse x)
;; the output should be ((4 3) (2 1))
答案
在前面的练习 2.18 中实现的 reverse 忽略了列表中的元素可能仍然是列表的情况。这里要求的实现的 deep-reverse 则要求我们递归式翻转整个列表。