Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Hamming Sequences and Lazy Lists

by tlm (Prior)
on Mar 17, 2005 at 23:06 UTC ( [id://440579]=note: print w/replies, xml ) Need Help??


in reply to Hamming Sequences and Lazy Lists

The challenge would be to do this in perl. I believe perl6 has or will soon be getting Haskell-like lazy infinite lists. Can it be done in perl5?

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

ll_new( $x, < any perl expression > )
had to be recast into
ll_new( $x, memoize( sub { < any perl expression > } ) )
The business with memoize is in some sense not essential; this would have, in principle, worked too:
ll_new( $x, sub { < any perl expression > } )

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:

$fibs = ll_new( 0, memoize ( sub { ll_new( 1, memoize ( sub { ll_add( tail( $fibs ), $fibs ); } ) ); } ) );
to the relatively pithy:
$fibs = ll_new( 0, ll_new( 1, ll_add( tail( $fibs ), $fibs ) ) );
which holds its own against the Haskell rendition of the same:
fibs = 0:1:[a+b| (a,b) <- zip fibs (tail fibs) ]

the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://440579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found