[Haskell-cafe] Lambda-case / lambda-if
Christopher Done
chrisdone at googlemail.com
Sat Oct 2 15:13:57 EDT 2010
On 2 October 2010 20:23, Max Bolingbroke <batterseapower at hotmail.com> wrote:
> Do you like this feature and think it would be worth incorporating
> this into GHC? Or is it too specialised to be of use? If there is
> enough support, I'll create a ticket and see what GHC HQ make of it.
Nice work! I like it and have wanted it for a while, and I know many
in the #haskell IRC channel would like it. The case is especially
useful. Maybe the if is only useful sometimes.
A benefit for lambda-case that I'll throw in the mix is:
main = do
args <- getArgs
case args of
[path] -> do exists <- doesFileExist filepath
if exists
then readFile filepath >>= putStrLn
else error "file does not exist"
_ -> error "usage: foo <filename>"
becomes:
main = do
getArgs >>= case of
[path] -> doesFileExist filepath
>>= if then readFile filepath >>= putStrLn
else error "file does not exist"
_ -> error "usage: foo <filename>"
There's nothing more annoying than having to introduce intermediate
bindings when you're going to immediate pattern match against it
immediately and never use it again. It's both annoying to have to
think of a variable name that makes sense and is not in scope or will
be in scope, and annoying to type it out, and it's just ugly. This is
*not* a special-case, it happens all the time and it's one of the few
things in the syntax I wish could be updated.
I vote yes, yes, and double yes!
More information about the Haskell-Cafe
mailing list