Haskell 中的多项式与地图有麻烦
我需要使用 map 将多项式乘以一个数字。我已经尝试了很长时间,但我已经快要疯了。我尝试了两种方法来解决此错误:
data Pol = P [(Float,Int)] deriving Show
prodEsc :: Pol -> Float -> Pol
prodEsc (P a) n = P (prodAux a n)
--First try:
prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) n -> (c*n,g)) xs
--error:
ERROR file:.\Febrero 2011.hs:21 - Type error in explicitly typed binding
*** Term : prodAux
*** Type : [(Float,Int)] -> Float -> [Float -> (Float,Int)]
*** Does not match : [(Float,Int)] -> Float -> [(Float,Int)]
--Second try:
prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux (x:xs) n = map (opera x n) (x:xs)
opera :: (Float,Int) -> Float -> (Float,Int)
opera (c,g) n = (c*n,g)
--error:
ERROR file:.\Febrero 2011.hs:23 - Type error in application
*** Expression : map (opera x n) (x : xs)
*** Term : opera x n
*** Type : (Float,Int)
*** Does not match : (Float,Int) -> a
有人可以帮助我吗?
太感谢了!!
I need to multiply a polynomial by a number using map. I´ve been trying for a long time and I´m already getting crazy. I´ve tried two ways getting this errors:
data Pol = P [(Float,Int)] deriving Show
prodEsc :: Pol -> Float -> Pol
prodEsc (P a) n = P (prodAux a n)
--First try:
prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) n -> (c*n,g)) xs
--error:
ERROR file:.\Febrero 2011.hs:21 - Type error in explicitly typed binding
*** Term : prodAux
*** Type : [(Float,Int)] -> Float -> [Float -> (Float,Int)]
*** Does not match : [(Float,Int)] -> Float -> [(Float,Int)]
--Second try:
prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux (x:xs) n = map (opera x n) (x:xs)
opera :: (Float,Int) -> Float -> (Float,Int)
opera (c,g) n = (c*n,g)
--error:
ERROR file:.\Febrero 2011.hs:23 - Type error in application
*** Expression : map (opera x n) (x : xs)
*** Term : opera x n
*** Type : (Float,Int)
*** Does not match : (Float,Int) -> a
Could anyone help me please?.
Thank you so much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用地图时,您不需要自己对列表是否为空进行模式匹配。所以我强烈怀疑,而不是
你的意思
只是否则你会扔掉第一对(c,g),这似乎没有多大意义。
When you're using map, you don't need to do your own pattern matching on whether the list is empty or not. So I strongly suspect that instead of
you mean simply
Otherwise you would be throwing away the first (c,g) pair, which does not appear to make much sense.
你的第一次尝试是正确的。只需从 lambda 中删除
n
即可。它已经在范围内并且不需要:map
的类型为(a -> b) -> [一]-> [b]
。这意味着,它需要一个列表并对每个元素应用一个函数。该函数必须只有一个参数:要操作的元素。Your first try is right. Just remove the
n
from the lambda. It is already in scope and not needed:map
has the type(a -> b) -> [a] -> [b]
. this means, that it takes a list of something and applies a function to every element. This function has to have exactly one argument: The element to operate over.