[Haskell-cafe] Verbosity of imperative code (was: Learning
greenrd at greenrd.org
Wed Dec 7 16:11:44 EST 2005
On Wednesday 07 December 2005 20:19, you wrote:
> On Wed, Dec 07, 2005 at 07:47:46PM +0000, Robin Green wrote:
> > > Some day you may thank for this verbosity, because it encourages
> > > you do program in a purely functional way making your program more
> > > friendly for SMP execution.
> > You are mistaken. The verbosity is necessary if you want "visual"
> > referential transparency, but not necessary if you only want pure
> > functional programming. Only the latter is helpful for optimisability. I
> > am hoping to write a paper on this topic.
> I am afraid I don't understand.
Let's say you want to write a function
seqPair :: (Monad m) => (m a, m b) -> m (a, b)
which returns a computation which does the left computation followed by the
right computation (i.e. it's like the sequence function, but for pairs
instead of lists).
In Haskell you could write this as:
seqPair mx my = do
x <- mx
y <- my
return (x, y)
However, wouldn't it be nice if we could write something like (warning:
seqPair $= (,)
or (the slightly less cryptic version)
seqPair x y $= (x, y)
This is not referentially transparent because it is not equivalent to
seqPair x y $= swap (y, x) where swap (a, b) = (b, a)
(can you see why not?)
But it _is_ semantically equivalent to the first definition I gave, which is
functionally pure, so it is also functionally pure (at least, when you
consider it as a "black box").
More information about the Haskell-Cafe