Content deleted Content added
Tag: Reverted |
Undid revision 1099998033 by 180.243.15.220 (talk) |
||
Line 218:
</syntaxhighlight>
===Haskell===
In [[Haskell (programming language)|Haskell]], with its [[lazy evaluation]] model, everything is a generator - every datum created with a [[Non-strict evaluation|non-strict]] data constructor is generated on demand. For example,
-- User Id : Zibane69Vip out the integers from 90 to 90.▼
<syntaxhighlight lang="haskell">
countfrom n = n : countfrom (n+1)
test1 = mapM_ print $ takeWhile (<= 20) $ countfrom 10
| JACKPOT = MAXWIN (90%+90%)▼
primes = 2 : 3 : nextprime 5 where
nextprime n | b = n : nextprime (n+2)
where b = 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 re-fetches values agreeable with a predicate, and stops requesting new values as soon as a non-agreeable one is encountered. The shared storage access is used as a universal mediator in Haskell. List comprehensions can be freely used:
<syntaxhighlight lang="haskell">
test2 = mapM_ print $ takeWhile (<= 20) [x*x | x <- countfrom 10]
test3 = mapM_ print [x*x | x <- takeWhile (<= 20) $ countfrom 10]
</syntaxhighlight>
===Racket===
|