I'm still a beginner in Haskell.
My code
secureDivide :: Int -> Int -> Maybe Int
secureDivide _ 0 = Nothing
secureDivide 0 _ = Nothing
secureDivide x y = Just (x `div` y)
addOne:: Maybe Int -> Maybe Int
addOne (Just n) = Just (n + 1)
Actually I don't have any problem and get the result I want with 'secureDivide'.
Example : secureDivide 10 0 -> Nothing
But when I try something like this :
mySecureNext (mySecureDiv 10 0) -> I have an 'Exception' and not 'Nothing'
Is there a way to handle an error to a message without import something with the if-else statement like if error = Nothing else Just ... (or other option) ?
Nothingcase in youraddOnefunction. A better pattern is to simply define youraddOnefunction asInt -> Maybe Int. Then you can utilize the fact thatMaybeis a monad and dosecureDivide >>= addOnemySecureNextsupposed to beaddOne?addOne = fmap (+1). The definition offmaptakes care of bothJustarguments andNothingarguments. Your current definition duplicates theJustlogic while ignoring theNothinglogic.secureDivide 0 _ = Nothing. There is no problem dividing 0 by a nonzero value, so it doesn't need to be caught and handled separately. (If you do handle it separately, the result should beJust 0, notNothing.)