[Skip To Content]


# fizz buzz in haskell

  1. main = putStrLn ( join ( map fizzbuzz [1..100] ) )
  2. join :: [String] -> String
  3. join [x] = x
  4. join (x:xs) = x ++ "\n" ++ ( join xs )
  5. fizzbuzz :: Integer -> String
  6. fizzbuzz x | ( mod x 15 ) == 0 = "Fizzbuzz"
  7. | ( mod x 3 ) == 0 = "Fizz"
  8. | ( mod x 5 ) == 0 = "Buzz"
  9. | otherwise = show x

And the third (first, second) of my Haskell programs. Fizz Buzz! I deliberately built the string in a different (and probably better) way than 99 bottles.

# 99 bottles in haskell

  1. main = putStrLn ( bottle 99 )
  2. bottle :: Integer -> String
  3. bottle 1 = ( bottle_string 1 ) ++ " on the wall, "
  4. ++ ( bottle_string 1 ) ++ ".\n"
  5. ++ "Go to the store and buy some more, "
  6. ++ ( bottle_string 99 ) ++ " on the wall."
  7. bottle x = ( bottle_string x ) ++ " on the wall, "
  8. ++ ( bottle_string x ) ++ ".\n"
  9. ++ "Take one down and pass it around, "
  10. ++ ( bottle_string ( x - 1 ) ) ++ " on the wall.\n\n"
  11. ++ bottle ( x - 1 )
  12. bottle_string :: Integer -> String
  13. bottle_string 1 = "1 bottle of beer"
  14. bottle_string x = ( show x ) ++ " bottles of beer"

And 99 bottles of beer in Haskell. The code style is a bit more like the usual Haskell style than my last effort, I think.

# fibonacci in haskell

  1. main = print( fib 10 )
  2. fib :: Int -> Int
  3. fib( 0 ) = 0
  4. fib( 1 ) = 1
  5. fib( x ) = fib( x - 1 ) + fib( x - 2 )

My first Haskell program! Looks very like the definition on Wikipedia.

More about this site...

Last Week’s Top 5 Albums (More »)

  1. The SinglesThe Bluetones
  2. Night on My SideGemma Hayes
  3. PlansDeath Cab for Cutie
  4. Slipway FiresRazorlight
  5. Brain Thrust MasteryWe Are Scientists