Haskell本地函数不在范围内

时间:2022-02-17 17:05:58

The following function is supposed to return the sum of all the polynomials in a list having a polynomial as a list of floats. (ie: 4x²+2x+1 would be [4,2,1] and 5x⁵+x+2 [5,0,0,0,1,2])

以下函数应该返回具有多项式作为浮点列表的列表中的所有多项式的总和。 (即:4x²+ 2x + 1将是[4,2,1]和5x⁵+ x + 2 [5,0,0,0,1,2])

psum :: [[Float]] -> [Float]
psum (x1:x2:xs) = psum (binpsum (x1, x2)):xs
psum x = x
    where   binpsum (x:xs) (y:ys) = x+y:binpsum (xs, ys) 
            binpsum (x) (y:ys) = x+y:ys
            binpsum (x:xs) (y) = x+y:xs

I'm getting

 Not in scope: ‘binpsum’

It's the first time I work with haskell so I'd guess there's something wrong in the way I use binpsum (x1, x2) since I can't find anything wrong in the where clause.

这是我第一次使用haskell,所以我猜我使用binpsum(x1,x2)的方式有问题,因为我在where子句中找不到任何错误。

Thanks!

2 个解决方案

#1


The where clause only provides bindings to the equation directly above (or to the left) of it. You can fix this by moving it up, under the first of equation of psum where it is actually used.

where子句仅提供与其正上方(或左侧)的等式的绑定。您可以通过向上移动来解决这个问题,在实际使用的psum方程式中。

Other than that, there are various additional misunderstandings that I'm seeing in your code:

除此之外,我在你的代码中看到了各种各样的误解:

  1. When a function is defined like f x y, it must be called like that too (not like f (x, y)).
  2. 当函数定义为f x y时,它也必须被调用(不像f(x,y))。

  3. If you want to pattern match against a single item in a list, you must enclose the item in square brackets to indicate that it is indeed the item that you want to bind, and not the list itself.
  4. 如果要对列表中的单个项目进行模式匹配,则必须将项目括在方括号中以指示它确实是要绑定的项目,而不是列表本身。

  5. If an argument to a function is a expression of more than one word/symbol, you usually need to enclose it in brackets (e.g. f (a + b) instead of f a + b).
  6. 如果函数的参数是多个单词/符号的表达式,则通常需要将其括在括号中(例如f(a + b)而不是f a + b)。

Here is a version of your code that compiles:

以下是编译的代码版本:

psum :: [[Float]] -> [Float]
psum (x1:x2:xs) = psum (binpsum x1 x2 : xs)
    where binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
          binpsum [x] (y:ys) = x+y : ys
          binpsum (x:xs) [y] = x+y : xs
psum [x] = x

#2


Your function binpsum is only in the scope of the second definition. You can rewrite it one of these ways:

您的函数binpsum仅在第二个定义的范围内。您可以通过以下方式之一重写它:

psum :: [[Float]] -> [Float]
psum (x1:x2:xs) = psum (binpsum x1 x2):xs
  where 
    binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
    binpsum (x) (y:ys) = x+y:ys
    binpsum (x:xs) (y) = x+y:xs
psum x = x

or better:

psum :: [[Float]] -> [Float]
psum lst = case lst of
   x1:x2:xs -> psum (binpsum x1 x2) : xs
   _        -> lst
 where 
   binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
   binpsum (x) (y:ys) = x+y : ys
   binpsum (x:xs) (y) = x+y : xs

EDIT: After some correction in your code, though, it would look like this: (I just made it compile, I don't know if it does what you intended)

编辑:在您的代码中进行一些更正后,它看起来像这样:(我刚刚编译,我不知道它是否符合您的意图)

psum :: [[Float]] -> [Float]
psum lst = case lst of
    x1:x2:xs -> psum $ binpsum x1 x2 : xs
    [x]      -> x
  where
    binpsum [x] (y:ys)    = x + y : ys
    binpsum (x:xs) [y]    = x + y : xs
    binpsum (x:xs) (y:ys) = x + y : binpsum xs ys

Note that binpsum (x:xs) (y:ys) is now at the end as [3] would match (x:xs) with x = 3 and xs = [].

请注意,binpsum(x:xs)(y:ys)现在位于末尾,因为[3]将匹配(x:xs),x = 3且xs = []。

#1


The where clause only provides bindings to the equation directly above (or to the left) of it. You can fix this by moving it up, under the first of equation of psum where it is actually used.

where子句仅提供与其正上方(或左侧)的等式的绑定。您可以通过向上移动来解决这个问题,在实际使用的psum方程式中。

Other than that, there are various additional misunderstandings that I'm seeing in your code:

除此之外,我在你的代码中看到了各种各样的误解:

  1. When a function is defined like f x y, it must be called like that too (not like f (x, y)).
  2. 当函数定义为f x y时,它也必须被调用(不像f(x,y))。

  3. If you want to pattern match against a single item in a list, you must enclose the item in square brackets to indicate that it is indeed the item that you want to bind, and not the list itself.
  4. 如果要对列表中的单个项目进行模式匹配,则必须将项目括在方括号中以指示它确实是要绑定的项目,而不是列表本身。

  5. If an argument to a function is a expression of more than one word/symbol, you usually need to enclose it in brackets (e.g. f (a + b) instead of f a + b).
  6. 如果函数的参数是多个单词/符号的表达式,则通常需要将其括在括号中(例如f(a + b)而不是f a + b)。

Here is a version of your code that compiles:

以下是编译的代码版本:

psum :: [[Float]] -> [Float]
psum (x1:x2:xs) = psum (binpsum x1 x2 : xs)
    where binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
          binpsum [x] (y:ys) = x+y : ys
          binpsum (x:xs) [y] = x+y : xs
psum [x] = x

#2


Your function binpsum is only in the scope of the second definition. You can rewrite it one of these ways:

您的函数binpsum仅在第二个定义的范围内。您可以通过以下方式之一重写它:

psum :: [[Float]] -> [Float]
psum (x1:x2:xs) = psum (binpsum x1 x2):xs
  where 
    binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
    binpsum (x) (y:ys) = x+y:ys
    binpsum (x:xs) (y) = x+y:xs
psum x = x

or better:

psum :: [[Float]] -> [Float]
psum lst = case lst of
   x1:x2:xs -> psum (binpsum x1 x2) : xs
   _        -> lst
 where 
   binpsum (x:xs) (y:ys) = x+y : binpsum xs ys
   binpsum (x) (y:ys) = x+y : ys
   binpsum (x:xs) (y) = x+y : xs

EDIT: After some correction in your code, though, it would look like this: (I just made it compile, I don't know if it does what you intended)

编辑:在您的代码中进行一些更正后,它看起来像这样:(我刚刚编译,我不知道它是否符合您的意图)

psum :: [[Float]] -> [Float]
psum lst = case lst of
    x1:x2:xs -> psum $ binpsum x1 x2 : xs
    [x]      -> x
  where
    binpsum [x] (y:ys)    = x + y : ys
    binpsum (x:xs) [y]    = x + y : xs
    binpsum (x:xs) (y:ys) = x + y : binpsum xs ys

Note that binpsum (x:xs) (y:ys) is now at the end as [3] would match (x:xs) with x = 3 and xs = [].

请注意,binpsum(x:xs)(y:ys)现在位于末尾,因为[3]将匹配(x:xs),x = 3且xs = []。