[Haskell-cafe] Language semantics

David House dmhouse at gmail.com
Fri Jun 29 16:09:18 EDT 2007


Andrew Coppin writes:
 > I wonder what the layout for that is... something like this?
 > 
 >   case foo of
 >     patter1
 >       | guard1 -> ...
 >       | guard2 -> ...
 >     pattern2
 >       | guard3 -> ...
 >       | guard4 -> ...

Something like that should work. If the patterns are more indented than the
'case', and the guards are more indented than the patterns, then the layout rule
should work fine. As always, once you get used to the way layout works,
everything is pretty intuitive.

 > Well, I'll have to go try it...

Always a nice solution to these kinds of problems!

 > I always thought of guards as being a nice shorthand for if-expressions 
 > - but if they can affect the order of pattern matching, clearly they are 
 > more drastically different than I realised. (Generally, I never ever use 
 > 'em!)

A useful rule to remember with guards is that "once you cross the equals sign,
you can't go back". So if one of your patterns matches and a guard on that
pattern is true, that right-hand side will be evaluated and there is no way to
fall back to another guard or pattern.

-- 
-David House, dmhouse at gmail.com


More information about the Haskell-Cafe mailing list