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
Perl
runrig suggested the techniques from Mark Dominus’s excellent Higher Order Perl. Using the Stream module from HOP’s freely available sample code, the sieve of Eratosthenes is
Output:
Python
Borrowing code from a gist by alexbowe, the sieve in Python using streams is
Output:
Other possibilities
In some languages, the stream pattern is invisible. Lazy evaluation is a feature of Haskell, for instance, so you could define
primesas