なぜだろうと思っていた
'((atom1) . atom2)
次の選択肢から不適切なリストです
'(atom1 . (atom2))
'((atom1) . atom2)
'(atom1 atom2)
(cdr '(atom1))
(cons 'atom1 '(atom2))
なぜだろうと思っていた
'((atom1) . atom2)
次の選択肢から不適切なリストです
'(atom1 . (atom2))
'((atom1) . atom2)
'(atom1 atom2)
(cdr '(atom1))
(cons 'atom1 '(atom2))
適切なリストは、空のリスト、または がデータム (リストなどの別の構造である可能性があります) を指し、 が別の適切なリストを指すcons
セルのいずれかです。詳しくはこちらをご覧ください。この例では:car
cons
cdr
'((atom1) . atom2)
atom2
はヌル リストではないため、不適切であることがわかります。他の例を見てみましょう。
; `(atom2)` is a list, so the whole expression is a list
'(atom1 . (atom2))
; it's a well-formed list of atoms
'(atom1 atom2)
; the `cdr` part of '(atom1) is the null list, which is also a proper list
(cdr '(atom1))
; consing an element at the head of a proper lists yields a proper list
(cons 'atom1 '(atom2))
不適切なリストとは、anyが次のpair
条件を満たす場合です。
(define (improper? pair)
(and (not (eq? (cdr pair) '()))
(not (pair? (cdr pair)))))
つまり、不適切なリストとは、任意のペアが別のペアまたは空のリスト以外のものであるリストです。
> (improper? '(atom1 . (atom2)))
#f
> (improper? '((atom1) . atom2))
#t
> (improper? '(atom1 atom2))
#f
> (improper? (cdr '(atom1)))
#f ;; (cdr '(atom1)) is not a pair - can't use my improper?
> (improper? (cons 'atom1 '(atom2)))
#f
または、「ペア」だけでなく、「もの」について逆に述べます。
(define (proper? thing) ;; the cdr of the last pair must be '()
(or (null? thing)
(and (pair? thing)
(proper? (cdr thing)))))