理系学生日記

おまえはいつまで学生気分なのか

問題2-28

木構造の根のみからなるリストを返せって問題。
appendが問題2-27で出てきていたので、使ってみた。

(define (fringe x)
  (define (fringe-make-list elem)
    (cond ((null? elem) ())
	  ((list? elem)
	   (append (fringe-make-list (car elem))
		   (fringe-make-list (cdr elem))))
	  (else (list elem))))
  (fringe-make-list x))

こんな風になる。

gosh> (define x (list (list 1 2) (list 3 4)))
x
gosh> (fringe x)
(1 2 3 4)
gosh> (fringe (list x x))
(1 2 3 4 1 2 3 4)