#lang racket
要素とリストからペアを作成する必要があります
ただし、 を取得するときは(cons 2 (list 1 2 3))
、ドットを(2 (1 2 3))
取得(2 . (1 2 3))
するにはどうすればよいですか?
構造を説明する構文、格納方法、表現方法には違いがありますdisplay
。
2 つの要素のリストの場合、それを表す 2 つの方法(1)
と(1 . ())
. このリストが表示されると、括弧が最も少ないリストが常に優先されます。したがって'(2 . (1 2 3))
、常に(2 1 2 3)
byとして出力されdisplay
ます。あなたがそれを望まない場合は、あなた自身をcons-write
好きにすることができます:
;; displays cons always as dotted
(define (cons-write x)
(if (pair? x)
(begin
(display "(")
(cons-write (car x))
(display " . ") ; spaces are important
(cons-write (cdr x))
(display ")"))
(write x)))
(cons-write '(1 2 3 4)) ; prints (1 . (2 . (3 . (4 . ()))))