创建多项式函数

时间:2019-04-23 12:02:11

标签: racket tail-recursion

在我的测试中,我被要求编写一个函数createPolynomial 将数字0,...,-1的列表作为参数,并返回a 功能。返回的函数取数字0并返回值 多项式0⋅0 +⋯+ -1⋅-1在0。为此,我使用了 内置的pl expt函数采用两个数字和,并返回^

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

这是部分代码:

(: 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)

我遇到错误-

define: empty body (after defns/decls) in: (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))
> 

2 个答案:

答案 0 :(得分:1)

为空表示主体中没有表达式。 通常,这意味着括号被放错了位置。 使用缩进!

我猜,你打算写:

(: 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))

答案 1 :(得分:-1)

上面的答案对我没有帮助(因为我正在用 pl 写) 所以这是我的解决方案:

(: 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
  )

您将 polyX(即 Num -> Num)称为“createPolynomial”的返回。

因此,当(在某些定义中)使用数字列表调用 createPolynomial 时,您再次调用使用数字定义的内容时,该数字将进入 polyX,它将使用列表和 x 来创建 poly 函数。

>