[Haskell-cafe] proposal: point free case expressions
Reiner Pope
reiner.pope at gmail.com
Thu Nov 5 15:24:10 EST 2009
2009/11/5 Sebastiaan Visser <sfvisser at cs.uu.nl>:
> Hello all,
>
> Wouldn't it be nice if we could write point free case statements?
>
> I regularly find myself writing down something like this:
>
>> myFunc = anotherFunc $ \x -> case x of
>> Left err -> print err
>> Right msg -> putStrLn msg
>
> We could really use a case statement in which we skip the scrutinee and make
> `(case of {})' be syntactic sugar for `(\x -> case x of {})'.
>
> So we could write:
>
>> myFunc = anotherFunc $ case of
>> Left err -> print err
>> Right msg -> putStrLn msg
>
> A minor syntactical addition, a big win!
>
> Cheers,
>
> --
> Sebastiaan Visser
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
Morten Rhiger implemented "Type-safe pattern combinators" [1], which
are essentially a library for pattern matching, entirely within
Haskell98. As an example, he implemented "anonymous pattern-matching"
with this library, which is similar to what you ask for. It would be
certainly be possible to implement your proposal with his library.
My library "first-class-patterns" [2] on Hackage essentially follows
Morten Rhiger's approach, but makes the types more understandable. I
implemented point free case expressions (the 'elim' function) and
monadic pattern matches (the 'mmatch' function) in version 0.2.0,
which I just uploaded. For instance, you could write
> import Data.Pattern
>
> ---- anonymous matching
> ex6 :: Show a => Either a String -> IO ()
> ex6 = elim $
> left var ->> print
> <|> right var ->> putStrLn
>
> -- monadic matching
> ex8 :: IO ()
> ex8 = mmatch getLine $
> cst "" ->> return ()
> <|> var ->> putStrLn . ("You said " ++)
Cheers,
Reiner
[1] http://www.itu.dk/people/mir/typesafepatterns.pdf
[2] http://hackage.haskell.org/package/first-class-patterns
More information about the Haskell-Cafe
mailing list