现在,仅仅通过查看问题以及变量L
和X
,我或多或少地会看到这些答案如何实现。
但是这次我想逐步了解这些答案是如何产生的。我的教授非常可怕,无法为他的生活解释任何事情。
L = (D B B A A A A C)
X = ((2B) (4A) (1C))
Q1) (cons (list (+ (caar X) 1) (cadar X)) (cdr X))
Answer: ((3B) (4A) (1C))
Q2) (cons (list 1 (car L)) X)
Answer: ((1D) (2B) (4A) (1C))
答案 0 :(得分:0)
您确定在X
的定义中没有错过空格吗?
看起来,它应该像这样定义:
X = ((2 B) (4 A) (1 C))
计算如下:
;;; Setup.
(defvar L '(D B B A A A A C))
(defvar X '((2 B) (4 A) (1 C)))
;;; Q1:
(cons (list (+ (caar X) 1) (cadar X)) (cdr X))
;;; ^^^^^^^^
;;; (caar X) = 2
(cons (list (+ 2 1) (cadar X)) (cdr X))
;;; ^^^^^^^
;;; (+ 2 1) = 3
(cons (list 3 (cadar X)) (cdr X))
;;; ^^^^^^^^^
;;; (cadar X) = B
(cons (list 3 'B) (cdr X))
;;; ^^^^^^^^^^^
;;; (list 3 'B) = (3 B)
(cons '(3 B) (cdr X))
;;; ^^^^^^^
;;; (cdr X) = ((4 A) (1 C))
(cons '(3 B) '((4 A) (1 C)))
;;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;;; (cons '(3 B) '((4 A) (1 C))) = ((3 B) (4 A) (1 C))
'((3 B) (4 A) (1 C))
;;; Q2:
(cons (list 1 (car L)) X)
;;; ^^^^^^^
;;; (car L) = D
(cons (list 1 'D) X)
;;; ^^^^^^^^^^^
;;; (list 1 'D) = (1 D)
(cons '(1 D) X)
;;; ^^^^^^^^^^^^^^^
;;; (cons '(1 D) X) = ((1 D) (2 B) (4 A) (1 C))
'((1 D) (2 B) (4 A) (1 C))