[Haskell-cafe] Re: Wikipedia on first-class object
lrpalmer at gmail.com
Fri Dec 28 10:25:11 EST 2007
On Dec 28, 2007 5:58 AM, Cristian Baboi <cristi at ot.onrc.ro> wrote:
> Here is how I want print to be in Haskell
> print :: (a->b) -> (a->b)
> with print = id, but the following "side effect":
> - I want to call the print function today, and get the value tomorrow.
You might be interested in the standard module Debug.Trace, which
defines a function:
trace :: String -> b -> b
Which allows you to print something avoiding the IO monad as you seem
to want. Then to get
your print function, all you would need is a Show instance for a -> b:
print f = trace (show f) f
Then the Show instance:
instance Show (a -> b) where
show f = "<function>"
And there you have it. It's not very useful, since it just prints
<function> for every function, but there you go. I suspect you want
to print some serialization of the function such that it can be read
in again and executed later. You're not gonna get that any time soon.
But I also suspect that there is another legitimate solution to your
problem. Unless the problem has to do with transferring *arbitrary*
functions (that is, your program knows nothing at all about what is
transferred) between processes (not to be confused with threads, which
Haskell is very good at).
More information about the Haskell-Cafe