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

On Sat, Sep 17, 2005 at 06:56:36PM +0100, Ben Rudiak-Gould wrote:
* The new syntax is really nice as a replacement for the annoyingly common "x <- foo ; case x of..." idiom that I've always disliked.
I might wish for "case of" to mean "\x -> case x of": foo >>= case of ... Useful outside of monads, eg to write anonymous functions for map. Andrew

On Mon, 19 Sep 2005, Andrew Pimlott wrote:
On Sat, Sep 17, 2005 at 06:56:36PM +0100, Ben Rudiak-Gould wrote:
* The new syntax is really nice as a replacement for the annoyingly common "x <- foo ; case x of..." idiom that I've always disliked.
I might wish for "case of" to mean "\x -> case x of":
foo >>= case of ...
Useful outside of monads, eg to write anonymous functions for map.
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 ...") Donn Cave, donn@drizzle.com

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} ? Regards, Sven Moritz

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.

On Tue, 20 Sep 2005, Bernard Pope wrote:
On Tue, 2005-09-20 at 10:14 +0200, Sven Moritz Hallberg wrote:
Donn Cave schrieb: ...
but how do you write getArgs >>= \ [] -> putStrLn "(no arguments)" (a:_) -> putStrLn (show a)
What about good old let?
main = getArgs >>= let f [] = putStrLn "(no arguments)" f (a:_) = putStrLn (show a) in f
Indeed, I think I'm even on record somewhere arguing that lambda is more or less extraneous. Over-used, at any rate. I was thinking "where" instead of "let", but same difference. But I wouldn't think the Haskell community would favor the Python approach, where they have lambda but don't mind that it's sort of crippled. If lambda is staying, and it's feasible to make it syntactically equivalent to a named function, that seems better than a new "caseof" that appears to be essentially the same thing. Donn Cave, donn@drizzle.com

On Mon, Sep 19, 2005 at 02:17:42PM -0700, Andrew Pimlott wrote:
On Sat, Sep 17, 2005 at 06:56:36PM +0100, Ben Rudiak-Gould wrote:
* The new syntax is really nice as a replacement for the annoyingly common "x <- foo ; case x of..." idiom that I've always disliked.
I might wish for "case of" to mean "\x -> case x of":
foo >>= case of ...
Useful outside of monads, eg to write anonymous functions for map.
Yes. this is exactly an extension I have wanted myself too. -- John Meacham - ⑆repetae.net⑆john⑈

On Mon, 19 Sep 2005, Andrew Pimlott wrote:
On Sat, Sep 17, 2005 at 06:56:36PM +0100, Ben Rudiak-Gould wrote:
* The new syntax is really nice as a replacement for the annoyingly common "x <- foo ; case x of..." idiom that I've always disliked.
I might wish for "case of" to mean "\x -> case x of":
foo >>= case of ...
You mean that 'case' should be a function with the selecting value as last argument? So e.g. (case of {Nothing -> g; (Just x) -> f x; }) would be equivalent to (maybe g f)
Useful outside of monads, eg to write anonymous functions for map.
Yes, this would be nice. This notation could even replace the current one to prevent from mistakes by mixing up both notations.
participants (6)
-
Andrew Pimlott
-
Bernard Pope
-
Donn Cave
-
Henning Thielemann
-
John Meacham
-
Sven Moritz Hallberg