[Haskell-cafe] case of (was: [Haskell] Mixing monadic and non-monadic functions)

Bernard Pope bjpop at cs.mu.OZ.AU
Tue Sep 20 04:51:33 EDT 2005


On Tue, 2005-09-20 at 10:14 +0200, Sven Moritz Hallberg wrote:
> Donn Cave schrieb:
> 
> > The ordinary lambda comes close - in ghc anyway, it supports
> > pattern matching.  But I can't work out the syntax for multiple
> > cases, which would obviously be needed to make it practically
> > useful.
> > 
> > e.g., this seems to be OK:
> >     getArgs >>= \ (a:_) -> putStrLn (show a)
> > 
> > but how do you write
> >     getArgs >>= \	[] -> putStrLn "(no arguments)"
> > 			(a:_) -> putStrLn (show a)
> > 
> > (pardon me if I missed where you were going in "case of ...")
> 
> Sorry, I'm just jumping into this discussion, but why shouldn't the
> above work? I.e. extend lambda to accept a group of patterns:
> 
> 	\{Pat1 -> exp1; Pat2 -> exp2; Pat3 -> exp3}

What about good old let?

main
   = getArgs >>=  let f []    = putStrLn "(no arguments)"
                      f (a:_) = putStrLn (show a)
                  in f

Bernie.



More information about the Haskell-Cafe mailing list