Generator/Exponential: Difference between revisions
→{{header|Haskell}}: Added type signatures, dropped a redundant $
(Added Rust) |
(→{{header|Haskell}}: Added type signatures, dropped a redundant $) |
||
Line 1,383:
Generators in most cases can be implemented using infinite lists in Haskell. Because Haskell is lazy, only as many elements as needed is computed from the infinite list:
<lang haskell>import Data.List.Ordered
powers :: Int -> [Int]
powers m = map (^ m) [0..]
squares = powers 2
cubes :: [Int]
▲squares = powers 2
cubes = powers 3
foo :: [Int]
foo = filter (not . has cubes) squares
main :: IO ()
main = print $ take 10 $ drop 20
▲{{out}}
<pre>[529,576,625,676,784,841,900,961,1024,1089]</pre>
|