fizz = cycle ["","","Fizz"]
buzz = cycle ["","","","","Buzz"]
fizzbuzz = zipWith (++) fizz buzz
fb = zipWith (\n cs -> if cs == "" then show n else cs) [1..] fizzbuzz
mapM_ putStrLn fb fizz = cycle ["","","Fizz"]
buzz = cycle ["","","","","Buzz"]
fizzbuzz = zipWith (++) fizz buzz
fb = zipWith (\n cs -> if cs == "" then show n else cs) [1..] fizzbuzz
mapM_ putStrLn fbPoetry.
In python:
from itertools import *
fizz = cycle(["", "", "fizz"])
buzz = cycle([""] * 4 + ["buzz"])
fizzbuzz = izip(fizz, buzz, xrange(1,100))
print "\n".join(map(lambda (x,y,z): x+y if x or y else str(z), fizzbuzz))
(bugs found by marekmroz fixed)Can you do [1,2,3] * 4 in haskell as well ?
Admittedly mine was not as elegant as I haven't thought of using izip with 3 iterables. Still, I don't think that lambda and islice is really needed... xrange(100) guarantees a finite number of iterations already.
from itertools import *
fizzer = cycle(['Fizz','',''])
buzzer = cycle(['Buzz','','','',''])
fizzbuzzer = izip(xrange(100), fizzer, buzzer)
for f in fizzbuzzer:
print f[1] + f[2] if f[1] or f[2] else f[0]
EDIT:
the output from your version does not look right. "Fizz" and "Buzz" are on the wrong index positions.
0
1
fizz
3
buzz
fizz
6
7
fizz
buzz
...You're also correct islice is not needed.
concat $ replicate 4 [1,2,3]