我拥有的矩阵是这样的:
(define matr '('("Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да")
'("Гълъб" "Не" "Не" "Не" "Да" "Не" "Да" "Не" "Да" "Да")
'("Котка" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не")
'("Куче" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не")))
我想在矩阵的末尾再添加一列。
我目前的代码是:
(define (matrix-add-column matr new-row new-matr)
(cond
[(null? matr) new-matr]
[(matrix-add-column (cdr matr) (cdr new-row) (cons new-matr (append (cadar matr) (car new-row))))]))
此命令(matrix-add-column matr '("Не" "Не" "Не" "Да") '())
的输出为:
'((((() "Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да" . "Не") "Гълъб" "Не" "Не" "Не" "Да" "Не" "Да" "Не" "Да" "Да" . "Не") "Котка" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не" . "Не")
"Куче"
"Да"
"Да"
"Да"
"Не"
"Не"
"Не"
"Не"
"Да"
"Не"
.
"Да")
我不喜欢它。我的新矩阵应该看起来像原始矩阵,但还有一列。如果列在原始矩阵上而不是副本上会更好。
如果你问为什么我会使用(cadar matr),那是因为
(car matr)
''("Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да")
(cadar matr)
'("Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да")
答案 0 :(得分:1)
假设你真的不希望你的矩阵成为引用的列表的列表,可能是这样的吗?
(define (matrix-add-column matrix column)
(map append matrix (map list column)))
示例:
> (define matr '(("Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да")
("Гълъб" "Не" "Не" "Не" "Да" "Не" "Да" "Не" "Да" "Да")
("Котка" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не")
("Куче" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не")))
> (matrix-add-column matr '("Не" "Не" "Не" "Да"))
'(("Шаран" "Не" "Не" "Не" "Не" "Да" "Не" "Не" "Не" "Да" "Не")
("Гълъб" "Не" "Не" "Не" "Да" "Не" "Да" "Не" "Да" "Да" "Не")
("Котка" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не" "Не")
("Куче" "Да" "Да" "Да" "Не" "Не" "Не" "Не" "Да" "Не" "Да"))