Scheme 方案中的多个不同错误
我在Scheme中做这个项目,这三种方法的错误让我非常困惑 方法#1: 我不知道为什么会发生这种情况,当我在添加任何内容之前检查它是否是一个列表时 方法#3 这个我不知道这是什么意思,什么是扩展 任何帮助都将不胜感激Scheme 方案中的多个不同错误,scheme,Scheme,我在Scheme中做这个项目,这三种方法的错误让我非常困惑 方法#1: 我不知道为什么会发生这种情况,当我在添加任何内容之前检查它是否是一个列表时 方法#3 这个我不知道这是什么意思,什么是扩展 任何帮助都将不胜感激 代码有(let()…),它的计算结果显然是列表?,因此额外的括号看起来很奇怪((let()+)12);==>3之所以有效,是因为让计算为一个过程,但是如果您尝试((cons 1’())12)则会出现一个错误,例如应用程序:(1)不是一个过程,因为(1)不是一个过程。还要知道,不区分
(let()…)
,它的计算结果显然是列表?
,因此额外的括号看起来很奇怪<代码>((let()+)12);==>3之所以有效,是因为让
计算为一个过程,但是如果您尝试((cons 1’())12)
则会出现一个错误,例如应用程序:(1)不是一个过程,因为(1)
不是一个过程。还要知道,不区分大小写是不推荐的,因此CONS
和REAL?
不是未来的证明
append
连接列表。它们必须是列表。在else
中,您知道因为lst
不是列表?
所以lst
不能是append
的参数<代码>缺点可能就是您要找的。由于列表在方案中是一种抽象魔法,所以我建议您熟悉配对。当我阅读(1233)
时,我看到(1.(2.(3)())
或者(cons1(cons2)(cons3'))
,你也应该这样做eval
在本规范中完全不合适。如果您通过(lambda(xy)(+xy))
,它的计算结果是一个操作过程,您可以执行(op12)
。直接使用OP
非常感谢。这很有帮助。这三条建议都奏效了。我的2的算法不起作用,但是CONS解决了语法问题。谢谢谢谢谢谢!!
; Returns the roots of the quadratic formula, given
; ax^2+bx+c=0. Return only real roots. The list will
; have 0, 1, or 2 roots. The list of roots should be
; sorted in ascending order.
; a is guaranteed to be non-zero.
; Use the quadratic formula to solve this.
; (quadratic 1.0 0.0 0.0) --> (0.0)
; (quadratic 1.0 3.0 -4.0) --> (-4.0 1.0)
(define (quadratic a b c)
(if
(REAL? (sqrt(- (* b b) (* (* 4 a) c))))
((let ((X (/ (+ (* b -1) (sqrt(- (* b b) (* (* 4 a) c)))) (* 2 a)))
(Y (/ (- (* b -1) (sqrt(- (* b b) (* (* 4 a) c)))) (* 2 a))))
(cond
((< X Y) (CONS X (CONS Y '())))
((> X Y) (CONS Y (CONS X '())))
((= X Y) (CONS X '()))
)))#f)
Error:
assertion-violation: attempt to call a non-procedure [tail-call]
('(0.0) '())
1>
assertion-violation: attempt to call a non-procedure [tail-call]
('(-4.0 1.0) '())
;Returns the list of atoms that appear anywhere in the list,
;including sublists
; (flatten '(1 2 3) --> (1 2 3)
; (flatten '(a (b c) ((d e) f))) --> (a b c d e f)
(define (flatten lst)
(cond
((NULL? lst) '())
((LIST? lst) (APPEND (CAR lst) (flatten(CDR lst))))
(ELSE (APPEND lst (flatten(CDR lst))))
)
)
Error: assertion-violation: argument of wrong type [car]
(car 3)
3>
assertion-violation: argument of wrong type [car]
(car 'a)
; Returns the value that results from:
; item1 OP item2 OP .... itemN, evaluated from left to right:
; ((item1 OP item2) OP item3) OP ...
; You may assume the list is a flat list that has at least one element
; OP - the operation to be performed
; (accumulate '(1 2 3 4) (lambda (x y) (+ x y))) --> 10
; (accumulate '(1 2 3 4) (lambda (x y) (* x y))) --> 24
; (accumulate '(1) (lambda (x y) (+ x y))) --> 1
(define (accumulate lst OP)
(define f (eval OP (interaction-environment)))
(cond
((NULL? lst) '())
((NULL? (CDR lst)) (CAR lst))
(ELSE (accumulate(CONS (f (CAR lst) (CADR lst)) (CDDR lst)) OP))
)
)
Error:
syntax-violation: invalid expression [expand]
#{procedure 8664}
5>
syntax-violation: invalid expression [expand]
#{procedure 8668}
6>
syntax-violation: invalid expression [expand]
#{procedure 8672}
7>
syntax-violation: invalid expression [expand]
#{procedure 1325 (expt in scheme-level-1)}