Generator/Exponential: Difference between revisions

added haskell
(→‎{{header|Python}}: Newly created..)
(added haskell)
Line 5:
 
'''Task:''' Write a generator (or generators), in the most natural way in your language, that produces the numbers that are squares (<math>n^2</math>) but not cubes (<math>m^3</math>) and use that to print the first 21st to 30th members of that sequence.
 
=={{header|Haskell}}==
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>powers m = map (^ m) [0..]
 
filtered (x:xs) (y:ys) | x > y = filtered (x:xs) ys
| x < y = x : filtered xs (y:ys)
| otherwise = filtered xs (y:ys)
 
squares = powers 2
cubes = powers 3
f = filtered squares cubes
 
main :: IO ()
main = print $ take 10 $ drop 20 $ f</lang>
 
'''Sample output'''
<pre>[529,576,625,676,784,841,900,961,1024,1089]</pre>
 
=={{header|Python}}==
Anonymous user