Haskell 中的多项式与地图有麻烦

发布于 2024-12-01 17:33:34 字数 1068 浏览 6 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-12-08 17:33:34

当您使用地图时,您不需要自己对列表是否为空进行模式匹配。所以我强烈怀疑,而不是

prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) n -> (c*n,g)) xs 

你的意思

prodAux xs n = map (\ (c,g) -> (c*n,g)) xs

只是否则你会扔掉第一对(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

prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) n -> (c*n,g)) xs 

you mean simply

prodAux xs n = map (\ (c,g) -> (c*n,g)) xs

Otherwise you would be throwing away the first (c,g) pair, which does not appear to make much sense.

好久不见√ 2024-12-08 17:33:34

你的第一次尝试是正确的。只需从 lambda 中删除 n 即可。它已经在范围内并且不需要:

prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) -> (c*n,g)) xs

map 的类型为 (a -> b) -> [一]-> [b]。这意味着,它需要一个列表并对每个元素应用一个函数。该函数必须只有一个参数:要操作的元素。

Your first try is right. Just remove the n from the lambda. It is already in scope and not needed:

prodAux :: [(Float,Int)] -> Float -> [(Float,Int)]
prodAux [] _ = []
prodAux ((c,g):xs) n = map (\ (c,g) -> (c*n,g)) xs

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文