|
Re: Guards (Was: Some random newbie questions): msg#00114lang.haskell.cafe
Jon Cast wrote: Absolutely. In Haskell's syntax, if-then-else-if interacts badly with No problem: select :: a -> [(Bool, a)] -> a select def = maybe def snd . List.find fst Use it this way: select defaultE [(p1, e1), (p2, e2)] Would be a nice Prelude function. parseCmd ln Really, the second alternative is cleaner in my opinion. Furthermore, guards are an extension of pattern matching, which means At least one guard can nicely be avoided: (x:xn) !! n = if n == 0 then x else xn !! (n - 1) But I see that guards can be used to let pattern matching fail. Pattern guards are also nice for implementing ‘views’: I don't know what this means exactly, but I think I can transform it formally to: balance e'@(LeftLeaning (LeftBalanced e:es)) = case balance (LeftLeaning es) of Balanced es' -> Balanced (e:es') _ -> e' This way it is more clear for me, that 'balance' can return something different from 'Balanced' and that the data is returned unchanged in this case. balance (LeftLeaning []) = Balanced [] Well, I could never do without them. Sometimes I see people abusing guards, e.g. they write a 'length x == 1' guard, where the pattern '[x0]' would be clearly the better choice. So I'm always thinking twice before using a guard. |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Some random newbie questions: 00114, Benjamin Pierce |
|---|---|
| Next by Date: | Re: The Implementation of Functional Programming Languages: 00114, Ivan Boldyrev |
| Previous by Thread: | Re: Guards (Was: Some random newbie questions)i: 00114, Jon Cast |
| Next by Thread: | Re: Guards: 00114, Henning Thielemann |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |