I am going through Learn you a haskell book, and in Chapter 8 there is a snippet of code which looks like this
data LockerState = Taken | Free deriving (Eq, Show) type Code = String type LockerMap = Map.Map Int (LockerState, Code) lookup' :: Int -> LockerMap -> Either String Code lookup' num_ map_ = case (Map.lookup num_ map_) of Nothing -> Left $ "LockerNumber doesn't exist!" Just (state, code) -> if state == Taken then Left $ "LockerNumber already taken!" else Right $ code
This works. However, I wanted to convert if/else block to guard statements like this:
lookup' :: Int -> LockerMap -> Either String Code lookup' num_ map_ = case (Map.lookup num_ map_) of Nothing -> Left $ "LockerNumber doesn't exist!" Just (state, code) -> | state == Taken = Left $ "LockerNumber already taken!" | otherwise = Right $ Code
This doesn't compile. It seems that usage of guards in Haskell is very restrictive/non intuitive. SO Ex1 SO Ex2. Is there a definite source which I can read which tells at which places I can use guards?
No, you can't. We all want it, but nobody can come up with a sensible syntax. Show activity on this post.
Haskell guards are used to test the properties of an expression; it might look like an if-else statement from a beginner's view, but they function very differently. Haskell guards can be simpler and easier to read than pattern matching .
It's not equivalent to _ , it's equivalent to any other identifier. That is if an identifier is used as a pattern in Haskell, the pattern always matches and the matched value is bound to that identifier (unlike _ where it also always matches, but the matched value is discarded).
Haskell : case expressions. A case expression must have at least one alternative and each alternative must have at least one body. Each body must have the same type, and the type of the whole expression is that type.
There are two places guards are allowed: function definitions and case
expressions. In both contexts, guards appear after a pattern and before the body, so you use =
in functions and ->
in case
branches, as usual:
divide x y | y == 0 = Nothing -------- | otherwise = Just (x / y) ----------- positively mx = case mx of Just x | x > 0 -> Just x ------- _ -> Nothing
Guards are simply constraints for patterns, so Just x
matches any non-Nothing
value, but Just x | x > 0
only matches a Just
whose wrapped value is also positive.
I suppose the definitive reference is the Haskell Report, specifically §3.13 Case Expressions and §4.4.3 Function and Pattern Bindings, which describe the syntax of guards and specify where they’re allowed.
In your code, you want:
Just (state, code) | state == Taken -> Left "LockerNumber already taken!" | otherwise -> Right code
This is also expressible with patterns alone:
Just (Taken, _) -> Left "LockerNumber already taken!" Just (_, code) -> Right code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With