[Haskell-cafe] Verbosity of imperative code (was: Learning
Haskell)
Tomasz Zielonka
tomasz.zielonka at gmail.com
Thu Dec 8 02:33:16 EST 2005
On Wed, Dec 07, 2005 at 09:11:44PM +0000, Robin Green wrote:
> 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:
> hypothetical syntax)
I'm not sure. I wouldn't be the first one to jump with joy if it was
allowed :-)
> seqPair $= (,)
>
> or (the slightly less cryptic version)
>
> seqPair x y $= (x, y)
Wouldn't they be different, the first one forcing (,) to WHNF (NOP), and
the second one forcing x and y to WHNF?
Would you want seqPair written with $= to still have a type involving
the Monad class?
> 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?)
Only guessing, because I am not sure what $= is supposed to do, but
is it because y would be evaluated before x? I can't find anything wrong
with it, besides the strange semantics of $=.
Anyway, I didn't think my initial statement was so controversial.
I even said "may", not "will".
Best regards
Tomasz
--
I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland
More information about the Haskell-Cafe
mailing list