EoPL reading (2) 1.2.4 Exercises
羽生くんのソレを見つつ。
1.2.4 - 1
繰返しなソレをバスで検討してみた。こんなカンジ。
(define (duple n x) (define (my-append x y) (if (null? x) (cons y '()) (begin (set-cdr! x (cons y '())) x))) (define (inner-duple rslt n x) (if (= n 0) rslt (inner-duple (my-append rslt x) (- n 1) x))) (inner-duple '() n x) )
がしかし、試験にはパスしない。
$ make Testing duple ... failed. discrepancies found. Errors are: test (duple 4 '(ho ho)) should return ((ho ho) (ho ho) (ho ho) (ho ho)): expects ((ho ho) (ho ho) (ho ho) (ho ho)) => got (#0=(ho ho) #0#) $
よく考えたらそうだよな、と言いつつ以下に修正
(define (duple n x) (define (my-append x y) (define (end-of-x x) (if (null? (cdr x)) x (end-of-x (cdr x))) ) (if (null? x) (cons y '()) (begin ; (set-cdr! x (cons y '())) (set-cdr! (end-of-x x) (cons y '())) x))) (define (inner-duple rslt n x) (if (= n 0) rslt (inner-duple (my-append rslt x) (- n 1) x))) (inner-duple '() n x) )
ケツに追加しないと駄目じゃん、が上記。なんですが結局再帰になってるんじゃないかと。end-of-x は末尾再帰になってるのかなぁ。trace してみりゃ分かるのか。