[Haskell-cafe] A syntax proposal: data replacements

ihope ihope127 at gmail.com
Wed Mar 8 21:20:50 EST 2006


I'd like to propose more syntactic sugar more Haskell. (A spoonful of
syntactic sugar makes the medicine go down...)

Put simply, this would provide a smallish bit of pattern matching, and
hopefully clarify some things. A simple example should pretty much
define the whole thing:

fromJust = {Just ->- id; Nothing ->- error "Nothing"}

This function takes a single value. If the constructor used to
construct it is Just, then the Just is replaced with id. Likewise, if
it's given Nothing, it will return (error "Nothing"). Another example:

consPair = {(,) ->- (:)}

This takes a pair (x,y) and replaces the constructor (,) with (:),
yielding (x:y). Now, a final clarification:

foldr cons nil = {(:) ->- cons; [] ->- nil}

This will *not* work: only the top bit of list will be replaced this
way, as data replacements are not recursive.

One last example: the function {Left ->- id; Right ->- error} takes an
Either a String and either pulls out the a and returns that or uses
the String as an error message.


More information about the Haskell-Cafe mailing list