Created
August 3, 2012 21:12
-
-
Save jbpotonnier/3251584 to your computer and use it in GitHub Desktop.
My fizz/buzz in Haskell and its direct translation in Python (inspired from http://www.free-variable.org/2009/07/were-not-in-kansas-anymore-toto/ ).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fizz = cycle ["", "", "fizz"] | |
buzz = cycle ["", "", "", "", "buzz"] | |
numbers = map show [1..] | |
combine "" "" number = number | |
combine "fizz" "buzz" _ = "Fizz buzz!" | |
combine "fizz" _ _ = "Fizz!" | |
combine _ "buzz" _ = "Buzz!" | |
fizzbuzz = zipWith3 combine fizz buzz numbers | |
main = do | |
print $ take 20 fizzbuzz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import count, cycle, imap, islice | |
fizz = cycle(['', '', 'fizz']) | |
buzz = cycle(['', '', '', '', 'buzz']) | |
numbers = imap(str, count(1)) | |
def combine(fizz, buzz, number): | |
if not fizz and not buzz: return number | |
if fizz == 'fizz' and buzz == 'buzz': return 'Fizz buzz!' | |
if fizz == 'fizz': return 'Fizz!' | |
if buzz == 'buzz': return 'Buzz!' | |
fizzbuzz = imap(combine, fizz, buzz, numbers) | |
if __name__ == '__main__': | |
print list(islice(fizzbuzz, 20)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment