Skip to content

Instantly share code, notes, and snippets.

@blerou
Created October 19, 2012 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blerou/3918200 to your computer and use it in GitHub Desktop.
Save blerou/3918200 to your computer and use it in GitHub Desktop.
Let's play Haskell! - 1st session
-- az elso nyers megvalositas szambol -> szoveg
fizzbuzzText n =
if n `mod` 3 == 0 then
if n `mod` 5 == 0 then
"FizzBuzz"
else
"Fizz"
else
if n `mod` 5 == 0 then
"Buzz"
else
show n
-- binding letrehozas let in konstrukcioval
fizzbuzzTextLet n =
let
fizz = if n `mod` 3 == 0 then "Fizz" else ""
buzz = if n `mod` 5 == 0 then "Buzz" else ""
in fizz ++ buzz
-- binding letrehozas where konstrukcioval
fizzbuzzTextWhere n = fizz ++ buzz
where
fizz = if n `mod` 3 == 0 then "Fizz" else ""
buzz = if n `mod` 5 == 0 then "Buzz" else ""
-- maga a fizzbuzz list comprehension-nel
fizzbuzzListComp = [fizzbuzzText n | n <- [1..]]
-- maga a fizzbuzz mappel
fizzbuzzMap = map fizzbuzzTextLet [1..]
-- alternativ megvalositas, vegtelen listak + zip
threes = cycle ["", "", "Fizz"]
fives = cycle ["", "", "", "", "Buzz"]
fizzbuzzZip = zipWith (++) threes fives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment