球拍中的代码错误
Error with code in racket
我有这个代码但是我得到这个错误:
application: not a procedure;
That procedure can expected to be applied to arguments
given: '(8 1 2 3 4 5)
arguments .:
我尝试修复它,但仍然没有结果。
此函数应该 return 一个新列表,将名为 "listas" 的列表的元素插入到名为 "indices" 的列表中包含的相应位置。
(define (insertar posicion elemento lista)
(cond ((= posicion 0) (cons elemento lista))
(else (cons (car lista) (insertar (- posicion 1) elemento (cdr lista))))
)
)
(define multi-insertar (lambda (lista listas indices)
(if (not (eq? (length listas) (length indices)))
"Numero de indice no es igual a numero de listas a ingresar"
(if (= 1 (length indices))
(insertar (car indices) (car listas) lista)
((insertar (car indices) (car listas) lista)
(multi-insertar lista (cdr listas) (cdr indices))
)
)
)
)
)
在 multi-insertar
的第 6 行和第 7 行中,您有:
(let ((operator (insertar (car indices) (car listas) lista))
(argument (multi-insertar lista (cdr listas) (cdr indices))))
(operator argument)) ; operator must be a procedure for this to work!
我已将这两个表达式放在 let
中,以使其更易于阅读。错误说明 operator
的值不是过程,但列表 (8 1 2 3 4 5)
和 ('(8 1 2 3 4 5) argument)
对计算器没有意义。
但是这样的代码确实有意义,因为这里的变量 operator
是一个过程:
(let ((operator (if (< x 1) * /))
(operator x 10)) ; we either divide or multiply by 10.
您可能想做的是:
(define (multi-insertar lista listas indices)
(if (not (eq? (length listas) (length indices)))
"Numero de indice no es igual a numero de listas a ingresar"
(if (= 1 (length indices))
(insertar (car indices) (car listas) lista)
;; only the following branch is changed
(insertar (car indices) (car listas)
(multi-insertar lista (cdr listas) (cdr indices))))))
所以现在 multi-insertar
从 lista
的末尾开始使用 insertar
插入所有元素。
评论:
elementos
更适合 listas
只有当索引按降序排列时,这才会按预期工作。比较以下两个例子:
> (multi-insertar '(a b c d e f) '(x y z) '(1 3 5))
=> (a x b c y d e z f)
> (multi-insertar '(a b c d e f) '(z y x) '(5 3 1))
=> (a x b y c z d e f)
我有这个代码但是我得到这个错误:
application: not a procedure;
That procedure can expected to be applied to arguments
given: '(8 1 2 3 4 5)
arguments .:
我尝试修复它,但仍然没有结果。
此函数应该 return 一个新列表,将名为 "listas" 的列表的元素插入到名为 "indices" 的列表中包含的相应位置。
(define (insertar posicion elemento lista)
(cond ((= posicion 0) (cons elemento lista))
(else (cons (car lista) (insertar (- posicion 1) elemento (cdr lista))))
)
)
(define multi-insertar (lambda (lista listas indices)
(if (not (eq? (length listas) (length indices)))
"Numero de indice no es igual a numero de listas a ingresar"
(if (= 1 (length indices))
(insertar (car indices) (car listas) lista)
((insertar (car indices) (car listas) lista)
(multi-insertar lista (cdr listas) (cdr indices))
)
)
)
)
)
在 multi-insertar
的第 6 行和第 7 行中,您有:
(let ((operator (insertar (car indices) (car listas) lista))
(argument (multi-insertar lista (cdr listas) (cdr indices))))
(operator argument)) ; operator must be a procedure for this to work!
我已将这两个表达式放在 let
中,以使其更易于阅读。错误说明 operator
的值不是过程,但列表 (8 1 2 3 4 5)
和 ('(8 1 2 3 4 5) argument)
对计算器没有意义。
但是这样的代码确实有意义,因为这里的变量 operator
是一个过程:
(let ((operator (if (< x 1) * /))
(operator x 10)) ; we either divide or multiply by 10.
您可能想做的是:
(define (multi-insertar lista listas indices)
(if (not (eq? (length listas) (length indices)))
"Numero de indice no es igual a numero de listas a ingresar"
(if (= 1 (length indices))
(insertar (car indices) (car listas) lista)
;; only the following branch is changed
(insertar (car indices) (car listas)
(multi-insertar lista (cdr listas) (cdr indices))))))
所以现在 multi-insertar
从 lista
的末尾开始使用 insertar
插入所有元素。
评论:
elementos
更适合 listas
只有当索引按降序排列时,这才会按预期工作。比较以下两个例子:
> (multi-insertar '(a b c d e f) '(x y z) '(1 3 5))
=> (a x b c y d e z f)
> (multi-insertar '(a b c d e f) '(z y x) '(5 3 1))
=> (a x b y c z d e f)