我被要求实现一个使用以下配置文件的功能:
maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer
并以下列方式响应:
> maybe_divide (Just 5) (Just 2)
Just 2
> maybe_divide (Just (-5)) (Just 2)
Just (-3)
> maybe_divide (Just (-5)) (Just 0)
Nothing
> maybe_divide Nothing (Just 1)
Nothing
> maybe_divide (Just 1) Nothing
Nothing
我已经写了以下内容,但它不会编译。你们有什么建议吗?
maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer
maybe_divide x y = case x of
Just x' -> case y of
Just y'
| y' == 0 -> Nothing
| otherwise -> x' `div` y'
Nothing -> Nothing