Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generator (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Haskell=== In [[Haskell (programming language)|Haskell]], with its [[lazy evaluation]] model, every datum created with a [[Non-strict evaluation|non-strict]] data constructor is generated on demand. For example, <syntaxhighlight lang="haskell"> countFrom :: Integer -> [Integer] countFrom n = n : countFrom (n + 1) from10to20 :: [Integer] from10to20 = takeWhile (<= 20) $ countFrom 10 primes :: [Integer] primes = 2 : 3 : nextPrime 5 where nextPrime n | notDivisible n = n : nextPrime (n + 2) | otherwise = nextPrime (n + 2) notDivisible n = all ((/= 0) . (rem n)) $ takeWhile ((<= n) . (^ 2)) $ tail primes </syntaxhighlight> where <code>(:)</code> is a non-strict list constructor, ''cons'', and <code>$</code> is just a ''"called-with"'' operator, used for parenthesization. This uses the standard adaptor function, <syntaxhighlight lang="haskell"> takeWhile p [] = [] takeWhile p (x:xs) | p x = x : takeWhile p xs | otherwise = [] </syntaxhighlight> which walks down the list and stops on the first element that doesn't satisfy the predicate. If the list has been walked before until that point, it is just a strict data structure, but if any part hadn't been walked through before, it will be generated on demand. List comprehensions can be freely used: <syntaxhighlight lang="haskell"> squaresUnder20 = takeWhile (<= 20) [x * x | x <- countFrom 10] squaresForNumbersUnder20 = [x * x | x <- takeWhile (<= 20) $ countFrom 10] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)