[Haskell-cafe] Functional progr., images, laziness and all the rest

Brian Hulley brianh at metamilk.com
Wed Jun 21 20:53:01 EDT 2006

jerzy.karczmarczuk at info.unicaen.fr wrote:
> you may transform a recurrential equation yielding Y out of X:
> Y[n+1] = a*X[N+1] + b*Y[n]
> usually (imperatively) implemented as a loop, into a stream
> definition:
> filtr a b x@(x0:xq) = y where
> y  = (x0:yq)
> yq = a*xq + b*y

Can you explain how this transformation was accomplished?
I don't see how
     yq = a * xq + b * y
relates to
    Y[n+1] = a*X[n+1] + b*Y[n]  -- (assuming the X[N+1] was a typo)

since y is a longer list than yq but Y[n] is an earlier element than Y[n+1], 
so it seems that the function is multiplying b by a later factor than it 

1) Someone reading the code needs to do a lot of work to try to recover the 
original equation
2) Wouldn't an imperative loop, using the original equation directly, have 
made everything much simpler?
3) Therefore laziness has lead to obfuscated code.

> with (*) and (+) conveniently overloaded (or replaced by specific
> obvious ops).
> In such a way you can program in 2 - 6 lines some quite exquisite
> musical instruments (for example the Karplus-Strong "guitar", or a
> flute), construct the reverberation filters, make ever-rising
> Shepard/Risset paradoxical sounds, etc. etc. With laziness it is a
> sheer pleasure and fun, without - a pain. If you wish, find my PADL talk 
> on it...
> In this context, I found Clean more helpful than Haskell, for ONE
> reason. Clean has a primitive datatype: unboxed, spine-lazy but
> head-strict lists. The co-recursion works, as the construction of the
> tail is postponed, but there is no pollution of the space by thunks -
> unevaluated list *elements*.
> This I really do miss in Haskell... But perhaps I simply don't know
> how to obtain a similar behaviour?

If you only needed the head-strict aspect, something like

     data HSList a = Empty | Cons !a (HSList a)

(GHC also has unboxed types so perhaps something like data HSList = Empty | 
Cons Double# HSList but see the restrictions on their use at 
http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html )

Regards, Brian.

Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list