首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方案中的多个不同误差

方案中的多个不同误差
EN

Stack Overflow用户
提问于 2020-02-25 17:38:47
回答 1查看 85关注 0票数 0

我正在计划中做这个项目,而这三种方法上的错误让我陷入了困境。

方法1:

代码语言:javascript
复制
; 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:

代码语言:javascript
复制
;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

代码语言:javascript
复制
; 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)}

这个我不知道这是什么意思,什么是扩展?

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-25 20:54:17

  1. 代码有明显的计算结果为list?(let () ...),因此额外的括号看起来很奇怪。((let () +) 1 2) ; ==> 3之所以有效,是因为let的计算结果是一个过程,但是如果您尝试使用((cons 1 '()) 1 2),您应该会发现说类似于application: (1) is not a procedure的话是错误的,因为(1)不是一个过程。也要知道,大小写不敏感是不可取的,所以CONSREAL?不是未来的证明。
  2. append级联列表。他们必须是名单。在else中,您知道因为lst不是list?,所以lst不能成为append的参数。cons可能就是你要找的东西。由于列表在Scheme中是抽象的、神奇的,所以我敦促您对配对感到满意。当我读(1 2 3)的时候,我看到了(1 . (2 . (3 . ())))或者(cons 1 (cons 2 (cons 3 '()))),你也应该看到。
  3. eval在这段代码中是完全不合适的。如果将计算为OP的(lambda (x y) (+ x y))传递给OP,则可以执行(OP 1 2)。直接使用OP .
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60400357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档