To implement streams as delayed lists in Lisp it's recommended to use Lisp macros.
(defmacro cons-stream (a b)
(cons ,a (delay ,b)))
(defmacro delay (expr)
`(memo-proc (lambda () ,expr)))
What would by Python and Perl way to do the same thing?
EDIT. Is it possible to use such a cool construct as streams
(define primes (sieve (integers-starting-from 2)))
in languages like Python and Perl
In Python, the closest structure would probably be a generator expression.
In Perl, there is not a native lazy list, but the language provides all of the primitives that are needed to build one. I have written a lazy list library called List::Gen which is available on CPAN.
use List::Gen '*';
my $primes = <2..>->filter(\&is_prime); # where you provide &is_prime
say "@$primes[0..10]"; # lazily finds the first 11 primes
the <2..>
bit could be written verbosely as range(2, 9**9**9)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With