Generator (computer programming): Difference between revisions

Content deleted Content added
added Category:Articles with example Haskell code
Moved Haskell into proper parent section ("uses" instead of "other implementations")
Line 213:
</source>
 
=== Haskell ===
In [[Haskell_(programming_language)|Haskell]], with its [[lazy evaluation]] model, everything is a generator - every datum created with non-strict data constructor that is, generated on demand. For example,
<source lang="haskell">
fibgen (a,b) = a : fibgen (b,a+b)
-- Main> take 10 (fibgen (0,1))
-- [0,1,1,2,3,5,8,13,21,34]
</source>
where <code>(:)</code> is a non-strict list constructor, ''cons''.
 
== Other Implementations ==
Line 295 ⟶ 303:
=== Lisp ===
[[Common Lisp]] also does not natively provide generators, yet various library implementations exist, such as [http://cliki.net/pygen pygen].
 
=== Haskell ===
In [[Haskell_(programming_language)|Haskell]], with its [[lazy evaluation]] model, everything is a generator - every datum created with non-strict data constructor that is, generated on demand. For example,
<source lang="haskell">
fibgen (a,b) = a : fibgen (b,a+b)
-- Main> take 10 (fibgen (0,1))
-- [0,1,1,2,3,5,8,13,21,34]
</source>
where <code>(:)</code> is a non-strict list constructor, ''cons''.
 
==See also==