在試圖找到如何轉換這樣的列表時,我碰到了 Scheme streams and circular lists。但是,該答案要求在雞場中沒有提供Racket中的功能。任何人都可以指示我如何在Chicken方案中做到這一點?或者以一種方案變種中立的方式?在Chicken方案中將列表轉換爲循環列表?
3
A
回答
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
2
當您使用SRFIs這是很簡單的:
(使用SRFI-1) (定義L「(1 2 3 4)) (申請圓形列表l)
相關問題
- 1. 將循環轉換爲列表理解?
- 2. 將鏈接列表轉換爲循環列表
- 3. 嘗試將鏈接列表轉換爲循環鏈接列表
- 4. 轉換For循環While循環(列表)
- 5. 任何方式將此循環轉換爲列表理解?
- 6. 將列表轉換爲案例類
- 7. 轉換的表情到列表方案
- 8. 將元素替換爲列表方案
- 9. 轉換鍵,值列表在循環
- 10. 將列表轉換爲foreach循環中的代理程序
- 11. 如何將for循環轉換爲Python中的理解列表?
- 12. 如何將矢量轉換爲方案中的列表
- 13. 在python中將列表和列表的列表轉換爲csv
- 14. 將列表轉換爲列表清單
- 15. 將列表轉換爲鏈接列表
- 16. 將列表從ord轉換爲列表
- 17. 將列表轉換爲字典列表
- 18. 將列表轉換爲一個列表
- 19. 將列表轉換爲整數列表
- 20. 將元組列表轉換爲列表?
- 21. 使用方案將元素插入到循環列表中
- 22. 將轉換列表轉換爲水星中的元組列表
- 23. 將列表理解轉換爲Common Lisp循環
- 24. 將靜態導航列表轉換爲WordPress的循環?
- 25. 試圖將python嵌套循環轉換爲列表/ dict理解
- 26. 如何將這個循環轉換爲列表理解?
- 27. 如何將此Python for循環轉換爲列表理解?
- 28. 使用循環將scala列表轉換爲json
- 29. 將for循環轉換爲列表理解
- 30. 將列表理解轉換爲while循環
祝福你,奧斯卡。 –
@xuinkrbin。謝謝,永遠是我的榮幸:) –