我正在计划中做这个项目,而这三种方法上的错误让我陷入了困境。
方法1:
; 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) '())我不知道它想叫什么。(0.0)和(-4.0 1.0)是我的预期输出,所以我不知道它试图做什么。
方法2:
;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)我不知道为什么会发生这种情况,当我在添加任何东西之前检查它是否是一个列表时。
方法3
; 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)}这个我不知道这是什么意思,什么是扩展?
任何帮助都将不胜感激。
发布于 2020-02-25 20:54:17
list?的(let () ...),因此额外的括号看起来很奇怪。((let () +) 1 2) ; ==> 3之所以有效,是因为let的计算结果是一个过程,但是如果您尝试使用((cons 1 '()) 1 2),您应该会发现说类似于application: (1) is not a procedure的话是错误的,因为(1)不是一个过程。也要知道,大小写不敏感是不可取的,所以CONS和REAL?不是未来的证明。append级联列表。他们必须是名单。在else中,您知道因为lst不是list?,所以lst不能成为append的参数。cons可能就是你要找的东西。由于列表在Scheme中是抽象的、神奇的,所以我敦促您对配对感到满意。当我读(1 2 3)的时候,我看到了(1 . (2 . (3 . ())))或者(cons 1 (cons 2 (cons 3 '()))),你也应该看到。eval在这段代码中是完全不合适的。如果将计算为OP的(lambda (x y) (+ x y))传递给OP,则可以执行(OP 1 2)。直接使用OP .https://stackoverflow.com/questions/60400357
复制相似问题