[Haskell-cafe] Haskell-cafe] Preventing sharing
Doug McIlroy
doug at cs.dartmouth.edu
Fri Jan 8 20:18:38 UTC 2016
Oleg rewrote my power-series one-liners to illustrate the fact that
lazy lists defined in a strict language need not be very clumsy to use.
I had implemented those funstions in many strict languages, one of
which was ML using a lazy-list implementation from Dave MacQueen.
But after I finally did it in Haskell, I never looked back.
Though ML was the best of the strict bunch. Haskell's overloading
and cleaner notation made for more perspicuous code. It also
enabled elegant code like this for the exponential series:
exps = 1 + integral exps
As Oleg pointed out, this won't work in a strict language, and
must be replaced with something much less vivid:
exps = fix (\s 1 + integral s)
In slightly more involved cases, such as
sins = integral coss
coss = 1 - integral sins
the strict equivalent becomes murky:
sins = fix (\s -> integral (1 - integral s))
coss = derivative sins
or
(sins, cosx) = fix (\sc -> (integral $ snd sc,
1 - integral $ fst sc)
Further, because lazy lists are a new type, they can't be
manipulated directly with the standard list vocabulary (map,
take, filter, etc.). All such functions must be lifted to the
new type. Thus, while lazy lists can be *programmed* in a
strict language, they *play* somewhat awkwardly in it.
In fairness, I must admit that the beauty of representation by
naked lists may not survive when power series are embedded in
a more comprehensive algebraic system. For example, if both
matrices and power series were defined as list instances of
Num, matrices might be confusEd with nested power series.
Combinations--matrices of power series and vice versa--could
be similarly ambiguous. But it could also turn out to cause
no more difficulty than the already extensive degree of
polymorphism in arithmetic expressions.
Doug
More information about the Haskell-Cafe
mailing list