小编B. *_*Zen的帖子

为什么将数据类型添加为类型声明的约束会导致匹配错误而不是更正确的错误?

或者,换句话说,是否需要添加如下所述的约束?

工作代码,函数f的最小类型声明:

data ZeroPositive = Zero | Positive Int deriving Show
f :: [Int] -> [ZeroPositive]
f [] = []
f (0:xs) = Zero:(f xs)
f (x:xs) = (Positive x):(f xs)
main = putStrLn . show $ f [0,2]
Run Code Online (Sandbox Code Playgroud)

结果:

[Zero,Positive 2]
Run Code Online (Sandbox Code Playgroud)

破坏的代码与无用的约束ZeroPositive:

data ZeroPositive = Zero | Positive Int deriving Show
f :: ZeroPositive => [Int] -> [ZeroPositive]
f [] = []
f (0:xs) = Zero:(f xs)
f (x:xs) = (Positive x):(f xs) …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass

4
推荐指数
1
解决办法
59
查看次数

标签 统计

haskell ×1

typeclass ×1