或者,换句话说,是否需要添加如下所述的约束?
工作代码,函数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)