そのようなリストを変換する方法を見つけようとして、 Scheme ストリームと循環リストに出くわしまし た。ただし、その答えには、チキンスキームでは利用できないラケットの機能が必要です。代わりにチキンスキームでこれを行う方法を教えてもらえますか? それともスキーム・バリアント・ニュートラルな方法で?
質問する
209 次
2 に答える
3
リストを変更できる場合、標準的な方法は次のとおりです。
(define (make-circular lst)
; helper for finding the last pair in a list
(define (last-pair lst)
(if (null? (cdr lst))
lst
(last-pair (cdr lst))))
; special case: if the list is empty
(cond ((null? lst) '())
(else
; set the last pair to point to the head of the list
(set-cdr! (last-pair lst) lst)
lst)))
上記は入力リストを変更することに注意してください。それ以外は、期待どおりに機能します。
(make-circular '(1 2 3 4 5))
=> #0=(1 2 3 4 5 . #0#)
(car (cdr (cdr (cdr (cdr (cdr (make-circular '(1 2 3 4 5))))))))
=> 1
于 2013-10-16T21:35:50.240 に答える