在模块中创建多项式函数-未绑定标识符

时间:2019-05-02 20:06:03

标签: racket tail-recursion

在我的测试中,我被要求编写一个函数createPolynomial,该函数以数字0,...,-1的列表作为参数,并返回一个输出函数。返回的函数取一个数字0,然后在0处返回多项式0⋅0 +⋯+ -1⋅-1的值。为此,我使用了内置的pl expt函数,该函数取了两个数字和,并返回^ < / p>

我得到了部分代码,我将在下面给出我的答案,并想在家中测试我的答案,但是尽管我的老师将我的答案标记为正确,但我无法使其运行。任何帮助将不胜感激

这是部分代码:

(: createPolynomial : (Listof Number) -> <-fill in->)
(define (createPolynomial coeffs)
 (: poly : (Listof Number) Number Integer Number ->
Number)
 (define (poly argsL x power accum)
 (if <-fill in->
 <-fill in->
 <-fill in-> )
  (: polyX : Number -> Number)
     (define (polyX x)
     fill in)
     fill in)

And here is my answer:

   (: createPolynomial : (Listof Number) -> (Number -> Number))
(define (createPolynomial coeffs)
  (: poly : (Listof Number) Number Integer Number ->
     Number)
  (define (poly argsL x power accum)
    (if (null? argsL)
        accum
    (poly (rest argsL) x (+ power 1) (+ accum (* (first argsL) (expt x power))))))
  (: polyX : Number -> Number)
  (define (polyX x)
    (poly coeffs x 0 0))
  (polyX x))



and here are some test for the code:

> (createPolynomial '(1 2 4 2))
- : (Number -> Number)
#<procedure:polyX>
(define p2345 (createPolynomial '(2 3 4 5)))
(test (p2345 0) =>
 (+ (* 2 (expt 0 0)) (* 3 (expt 0 1)) (* 4 (expt 0 2)) (* 5
(expt 0 3))))
(test (p2345 4) =>
 (+ (* 2 (expt 4 0)) (* 3 (expt 4 1)) (* 4 (expt 4 2)) (* 5
(expt 4 3))))
(test (p2345 11) => (+ (* 2 (expt 11 0)) (* 3 (expt 11 1)) (* 4
(expt 11 2)) (* 5 (expt 11 3))))
(define p536 (createPolynomial '(5 3 6)))
(test (p536 11) => (+ (* 5 (expt 11 0)) (* 3 (expt 11 1)) (* 6
(expt 11 2))))
(define p_0 (createPolynomial '()))
(test (p_0 4) => 0)

我得到的错误是:

x: unbound identifier in module in: x

我猜想它必须在括号中做些什么,但我不知道在哪里以及在什么地方...

1 个答案:

答案 0 :(得分:2)

key_columns是一个返回类型createPolynomial的函数-它是 curried 。您将返回应用 (Number -> Number),其中(polyX x)确实是未绑定的。通过将其包装在lambda中,将其转换为函数:

x

...,这5个测试通过。