Teo*_*off 1 haskell functional-programming
我想编写一个消耗Either值的函数和两个函数.它仅应用基于哪一个与内部类型兼容的功能.完全努力实施这一个.
mapAorB :: (x -> z) -> (y -> w) -> Either x y -> Either z w
Run Code Online (Sandbox Code Playgroud)
首先写出函数的所有参数
mapAorB :: (x -> z) -> (y -> w) -> Either x y -> Either z w
mapAorB xz yw eitherxy = undefined
Run Code Online (Sandbox Code Playgroud)
然后确定是否可以匹配任何参数.在这里,只有Either可以.将参数扩展为模式案例:
mapAorB xz yw (Left x) = undefined
mapAorB xz yw (Right y) = undefined
Run Code Online (Sandbox Code Playgroud)
根据函数的规范,我们知道如果我们输入Left x那么我们将得到Left z,同样如果我们输入Right y它将返回Right w.这给了一个额外的提示:
mapAorB xz yw (Left x) = Left z where z = undefined
mapAorB xz yw (Right y) = Right w where w = undefined
Run Code Online (Sandbox Code Playgroud)
所以,你会如何定义z和w?