14. FizzBuzz Rule
● Write a program that prints the
numbers from 1 to 100.
● For multiples of three print “Fizz”
instead of the number
● For multiples of five print “Buzz”.
● For numbers which are multiples of
both three and five print “FizzBuzz”
16. Haskell :: Maybe Monads FizzBuzz
type IsFizzBuzz = Maybe String
is_n:: Integer -> Integer -> Integer
is_n x y = x `mod` y
fizz :: Integer -> Integer
fizz x = is_n x 3
buzz :: Integer -> Integer
buzz x = is_n x 5
maybe_div :: Integer -> String -> IsFizzBuzz ->
IsFizzBuzz
maybe_div 0 str m = case m of
Nothing -> Just str
Just x -> Just $ str ++ x
maybe_div _ str m = m
maybe_fizz :: Integer -> IsFizzBuzz -> IsFizzBuzz
maybe_fizz x m = maybe_div is_fizz "Fizz" m
where is_fizz = fizz x
maybe_buzz :: Integer -> IsFizzBuzz -> IsFizzBuzz
maybe_buzz x m = maybe_div is_buzz "Buzz" m
where is_buzz = buzz x
maybe_fizzbuzz :: Integer -> IsFizzBuzz -> String
maybe_fizzbuzz x m = case m of
Nothing -> show x
Just x -> x
maybe_process :: Integer -> String
maybe_process x = maybe_fizzbuzz x =<< is_fizzbuzz x
where is_fizzbuzz x = maybe_fizz x >>= maybe_buzz x
go :: (Integer -> String) -> IO ()
go f = process f 1
process :: (Integer -> String) -> Integer -> IO ()
process f 101 = putStr ""
process f x = (putStrLn $ f x) >> process f next
where next = x + 1
19. Basic
# begin code
for i in range(1, 100):
if (i % 15 == 0):
print "FizzBuzz"
if (i % 3 == 0):
print "Buzz"
if (i % 5 == 0):
print "Fizz"
print i
# end
37. TRUE Pythonista
don’t use *“List”
in FizzBuzz.
* Strictly speaking,
“String” has “List”(= array (= char sets)) Structure,
but now, String is exception.