[Haskell-cafe] Sugar for function application

Ertugrul Soeylemez es at ertes.de
Tue Mar 23 13:39:27 EDT 2010


Hello cafe,

I have just had a discussion on #haskell about whether it would be
useful to have syntactic sugar for exception handling.  This and
syntactic sugar in general is a very controversial topic, seeing that
'do' notation is used a lot, where applicative/combinator style would be
more appropriate.  Some people would call this "abuse".

Let me tell you why I would like syntactic sugar, first.  Haskell is in
many regards very beautiful and the layout of code expresses its
structure.  However, when it comes to exception handling I have two
options:  use either parentheses or infix operator style.  For my taste
both get ugly, as soon as you have more than just an action and its
small exception handler.  At some point you end up using 'let' and/or
'where' to introduce extra names, something which you would want to
avoid in general, unless it really adds value to your code.  Currently
you introduce names just to make the code look nicer.  I find this very
unfortunate.

However, because it is such a controversial topic whether or not to add
syntactic sugar for exceptions, I'm proposing an alternative, which
seems to be nicer, because it doesn't introduce extra syntax for
specific things (like exceptions) and even has the potential to get rid
of some keywords (most notably 'if', 'then' and 'else') without making
code look worse:  layout-style syntactic sugar for function application.
Here is an example of what it might look like:

  function $$ anArgument
              sin (x^2)
              anotherArgument
              f $ x + 3

  printf $$ "%s: %s"
            key
            values ! key

  -- Assuming that 'if' is just a function:
  -- if :: Bool -> a -> a -> a
  if $$ even x
        x^2
        (x - 1)^2

  catch $$
    do someOperation
       someOtherOperation
       yetAnotherOperation
    \exception -> putStrLn "Error:" >> print exception

  bracket $$
    openFile "blah" ReadMode
    \h -> hClose h >> putStrLn "File closed."
    \h -> do
      putStrLn "File opened."
      doSomethingWithHandle h

Instead of the symbolic ($$) I could also think of 'with' or 'of' as
appropriate keywords, although 'of' would be ambiguous:

  catch with
    do someOperation
       someOtherOperation
       yetAnotherOperation
    \exception -> putStrLn "Error:" >> print exception

Also you should be able to use do-style semicolons:

  printf with "%s: %s"; key; values ! key

What do you think?  I would love to see this '$$' or 'with' construct as
a GHC extension.


Greets
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Haskell-Cafe mailing list