简体   繁体   English

在球拍中创建评估函数

[英]Creating an evaluate function in racket

在此处输入图片说明 Example of what function should do: (list 3 4 6 9 7) ←→ 3x^4 + 4x^3 + 6x^2 + 9x + 7函数应该做什么的例子:(list 3 4 6 9 7) ←→ 3x^4 + 4x^3 + 6x^2 + 9x + 7

What I have so far:到目前为止我所拥有的:

(define (poly-eval x numlist)
(compute-poly-tail x numlist 0 0))

(define (compute-poly-tail xn list n acc)
    (cond
      [(null? list) acc]
    [else (compute-poly-tail (first list) (rest list)
                       (+ acc (* (first list) (expt xn n))) (+ n 1))]))



(check-expect(poly-eval 5 (list 1 0 -1)) 24)
(check-expect(poly-eval 0 (list 3 4 6 9 7)) 7)
(check-expect(poly-eval 2 (list 1 1 0 1 1 0))  54)

Expected results:预期成绩:

(check-expect(poly-eval 5(list 1 0 -1)) 24)
(check-expect(poly-eval  0 (list 3 4 6 9 7))7)
(check-expect(poly-eval 2 (list 1 1 0 1 1 0)) 54)

I am getting a run-time error.我收到运行时错误。 Can someone spot what I am doing wrong.有人可以发现我做错了什么。 I don't know why I am getting these results.我不知道为什么我会得到这些结果。

Build power coefficient and unknown list than use map function.建立功率系数和未知列表,而不是使用地图功能。

; 2*3^1+4*3^0
; input is 3 and '(2 4)
; we need '(3 3) '(2 4) '(1 0)
; use map expt build '(3^1 3^0)
; use map * build '(2*3^1 4*3^0)
; use foldr + 0 sum up

(define (poly-eval x coefficient-ls)
  (local ((define power-ls (reverse (build-list (length coefficient-ls) values)))
          (define unknown-ls (build-list (length coefficient-ls) (λ (i) x))))
    (foldr + 0 (map * coefficient-ls (map expt unknown-ls power-ls)))))


There are a couple of errors in the code:代码中有几个错误:

  • You need to process the coefficient's list in the correct order, corresponding to their position in the polynomial!您需要以正确的顺序处理系数列表,对应于它们在多项式中的位置! you can either:你可以:
    • reverse the list from the beginning and process the coefficients from right to left (simpler).从头开始reverse列表并从右到左处理系数(更简单)。
    • Or start n in (sub1 (length numlist)) and decrease it at each iteration (that's what I did).或者在(sub1 (length numlist))开始n并在每次迭代时减少它(这就是我所做的)。
  • The order and value of the arguments when calling the recursion in compute-poly-tail is incorrect, check the procedure definition, make sure that you pass along the values in the same order as you defined them, also the first call to (first list) doesn't make any sense.compute-poly-tail调用递归时参数的顺序和值不正确,检查过程定义,确保按照定义它们的相同顺序传递值,也是第一次调用(first list)没有任何意义。
  • You should not name list a parameter, this will clash with the built-in procedure of the same name.你不应该list一个参数,这会与同名的内置过程发生冲突。 I renamed it to lst .我将它重命名为lst

This should fix the issues:这应该可以解决以下问题:

(define (poly-eval x numlist)
  (compute-poly-tail x numlist (sub1 (length numlist)) 0))

(define (compute-poly-tail xn lst n acc)
  (cond
    [(null? lst) acc]
    [else (compute-poly-tail xn
                             (rest lst)
                             (- n 1)
                             (+ acc (* (first lst) (expt xn n))))]))

It works as expected:它按预期工作:

(poly-eval 5 (list 1 0 -1))
=> 24

(poly-eval 0 (list 3 4 6 9 7))
=> 7

(poly-eval 2 (list 1 1 0 1 1 0))
=> 54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM