[Haskell-cafe] Alternative lambda syntax (not entirely serious)
Fritz Ruehr
fruehr at willamette.edu
Fri Feb 18 18:41:59 EST 2005
I was introducing a fresh set of students to lambda abstraction in
Haskell yesterday and had the following inspiration about a possible
alternative syntax. I didn't end up showing the idea to the students --
too confusing -- but I thought I would float it here, as long as nobody
takes it too seriously (this is the Cafe, after all :) ) .
The idea arose from two things I'd done recently in class: introducing
the underscore pattern as "an anonymous variable", and also commenting
on the relationship between the final "->" in a type signature and the
"=" of the corresponding function definition.
So, since a lambda abstraction denotes an anonymous function, the
underscore signifies anonymous things, and given that "->" of the
current syntax connotes a type than a value, I thought "why not this
syntax instead?":
(_ x = x) -- the identity function
(_ (x,y) = x) -- the fst function
(_ f g x = f (g x)) -- composition
In other words, at a lexical level we simply write "_" in place of "\"
and "=" in place of "->". The use and placement of the "_" nicely
indicates that what we are denoting is both anonymous and a function,
and the use of the "=" makes it more parallel to the definition of a
named function.
This looks pretty nice in the right context, e.g., when talking about
the kinds of code transformation one does when introducing lambda. For
example:
foo = map f xs
where f x = blah
VERSUS
foo = map (_ x = blah) xs
OK, so why not a serious proposal? Well first off, from a pedagogical
perspective, we may not *want* to stress the similarity between the
definition of a function and an expression which denotes one: this is a
point of some confusion for newcomers, after all. Second, I imagine it
could make parsing more difficult (although an *initial* underscore
wouldn't otherwise come up in an expression context, right?).
Third (and this is probably the death-knell), the use of "_" and "="
would be especially confusing in cases where an abstraction appears
immediately on the RHS of a definition, or where one of the parameter
patterns was itself an underscore. Or (and especially!) both; for
example:
const = (_ x _ = x) -- bleah!
or worse yet:
const x = (_ _ = x) -- yikes!
Now, such definitions don't come up much in actual production code, I
suppose, but they do tend to come up in classrooms, where the basic
ideas are being introduced, and confusion would be especially unwelcome
there.
Finally, and not least, this proposal would further distance Haskell's
syntax from the original lambda notation: this is not only a sad thing
in general, but it would also make lambda-based T-shirt & merchandise
logos even more obscure than they are now :) .
-- Fritz
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2838 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20050218/e3bc427e/attachment.bin
More information about the Haskell-Cafe
mailing list