[Haskell-cafe] infinite lists
Albert Y. C. Lai
trebla at vex.net
Tue Mar 27 15:17:05 EDT 2007
Matthias Fischmann wrote:
> here is the bug, narrowed to four lines. a function that only
> sometimes terminates.
> f = do
> n <- randomRIO (0,5)
> let l = replicate n '*'
> i = join $ repeat l -- infinite extension
> print (take 12 i)
In this paragraph I speak about just this program, ignoring that it is a
lossy excerpt from another program for a different specification. This
program, when corrected, will output the empty string with probability
1/6, and/or the string of twelve *'s with probability 5/6. This could be
done more simply and less error-prone by writing "if n==0 then the
former else the latter". In other words, I wonder why more information
is produced (in the sense of information theory and entropy), only to be
discarded, i.e., in 5/6 of the cases, you ask for 1 to 5 *'s, then
discard that length and make them all 12. Along with the introduction of
unnecessary work comes the introduction of unnecessary mistakes.
In this paragraph I acknowledge that this program is a lossy excerpt
from another program for a different specification. Still, if there is a
sane way of mapping this program back to its origin, there is probably
also some partial way of mapping my comment above to the original
specification and program. I advise you to review your specification and
program, and look for opportunities for eliminating unnecessary or
indirect work; you may accidentally eliminate some mistakes along the way.
More information about the Haskell-Cafe