[Haskell-beginners] Unwrap an expression?
byorgey at seas.upenn.edu
Sat Sep 3 02:19:04 CEST 2011
On Fri, Sep 02, 2011 at 02:23:40PM -0800, Christopher Howard wrote:
> Perhaps this is a dumb question, but I was pondering the whole "Lazy"
> aspect of Haskell, and was curious: is there someway you could create
> a function "M", which you could pass an expression into, so that...
> M (f (g... (n (x)) ...)) returns f
> and/or returns the inner value (g... (n (x)) ...)
> I don't know if that breaks the whole "referential transparency" idea
> or not, but with all those lambda calculus tricks I've heard about,
> and the lazy evaluation, I thought it would be worth asking.
It's a good question. You cannot do this, and you're right that it
breaks referential transparency. For example, suppose we have
f x = x + 3
Now using our mythical M function, we should be able to do
M (f 6)
and get f (or 6) as the result. But f 6 = 9 so we should be able to
replace it, like so:
but what is M 9? An error? In general, anything that works by
inspecting the *syntax* of expressions will break referential
Even more simply, however, you might also want to consider: what would
the type of M be?
More information about the Beginners