|Perl: the Markov chain saw|
Re: Hamming Sequences and Lazy Listsby tlm (Prior)
|on Mar 17, 2005 at 23:06 UTC||Need Help??|
To address this question more directly, I think it'd take very little to make this possible in Perl5. All that is required is a special function to mimic the ':' operator in Haskell. The crucial requirement for this operator is that its last operand not be evaluated unless it is specifically requested.
In what I posted, I had to do this "by hand"; i.e. what should have been simply
had to be recast into
The business with memoize is in some sense not essential; this would have, in principle, worked too:
Wrapping the second argument with an anonymous sub effectively delays its evaluation. But without the memoization, this whole scheme becomes hopelessly bogged down with all the recursive calls.
If perl did not evaluate the second argument of ll_new, then the whole implementation would look a lot cleaner. For example, the definition of $fibs above would go from:
to the relatively pithy:
which holds its own against the Haskell rendition of the same:
the lowliest monk