SICPの勉強 問題2.17~18
題意的に再帰を使って組み上げていくのがポイントなのだと思うが、やっぱり面倒くさい。
この章で作り上げていくmapやreduceといった抽象的にリストを捉えられる関数の偉大さを実感出来るのが最大の収穫だろうか。
2.17
リストの最後の要素を取り出す関数:
(defn last-pair [items] (loop [previous (first items) remaining (rest items)] (if (empty? remaining) (list previous) (recur (first remaining) (rest remaining)))))
2.18
リストの要素を逆順にする関数:
(defn reverse [items] (loop [items items result nil] (if (empty? items) result (recur (rest items) (cons (first items) result)))))