仕事がないからプログラミングGaucheを勉強中。
P53のcopy-listを参考にdeep-copy-listを作成してみた。
;copy-list (define (copy-list lis) (if (pair? lis) (cons (car lis) (copy-list (cdr lis))) lis))
;deep-copy-list (define (deep-copy-list lis) (if (pair? lis) (if (pair? (car lis)) (cons (deep-copy-list (car lis)) (deep-copy-list (cdr lis))) (cons (car lis) (deep-copy-list (cdr lis)))) lis))
あってんのかな?
追記
2個目のif文の辺りに無駄があったから直してみた。
;deep-copy-list (define (deep-copy-list lis) (if (pair? lis) (cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car lis)) (deep-copy-list (cdr lis))) lis))
やっぱりあってるかどうかわからん・・・
追記2
実行結果見たけどあってるっぽい
copy-list '(a b (c d) e) #?="./practice_p053.scm":3:(cons (car lis) (copy-list (cdr lis))) #?="./practice_p053.scm":3:(cons (car lis) (copy-list (cdr lis))) #?="./practice_p053.scm":3:(cons (car lis) (copy-list (cdr lis))) #?="./practice_p053.scm":3:(cons (car lis) (copy-list (cdr lis))) #?- (e) #?- ((c d) e) #?- (b (c d) e) #?- (a b (c d) e) deep-copy-list '(a b (c d) e) #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?- (d) #?- (c d) #?="./practice_p053.scm":8:(cons (if (pair? (car lis)) (deep-copy-list (car lis)) (car l ... #?- (e) #?- ((c d) e) #?- (b (c d) e) #?- (a b (c d) e)