Generators in Haskell
We translate the in-order tree traversal example from an old article Generators in Icon, Python, and Scheme, 2004.
http://okmij.org/ftp/Scheme/enumerators-callcc.html#Generators
using Haskell and delimited continuations rather than call/cc + mutation. The code is shorter, and it even types. To be honest, we actually translate the OCaml code generator.ml
In this code, we use a single global prompt (that is, ordinary shift0) Generator2.hs shows the need for several prompts.
Documentation
make_full_tree :: Int -> TreeSource
type P m a = PS (Res m a)Source
In Python, yield
is a keyword. In Haskell, it is a regular function.
Furthermore, it is a user-defined function, in one line of code.
To get generators there is no need to extend a language.