Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from srbaker/gist:b82ff8f170f1c71f30e9
Last active August 29, 2015 14:01
Show Gist options
  • Save jbrains/2a29465457bddcca0c45 to your computer and use it in GitHub Desktop.
Save jbrains/2a29465457bddcca0c45 to your computer and use it in GitHub Desktop.

Revisions

  1. jbrains revised this gist May 7, 2014. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    -- Thanks to @kimwallmark for teaching me `maybe`.
    -- I'd much rather use something like `lookup` instead of this hand-rolled `matchValue`.
    -- (Typing this last sentence gave me an idea.)
    -- Thanks to @kimwallmark for teaching me `maybe` and showing me how a single lookup.
    -- I had the brilliant idea of using `snd`, even though I dislike the name. :)

    fizzbuzz :: Integer -> String
    fizzbuzz n = maybe (show n) matchValue $ classify n
    fizzbuzz n = maybe (show n) snd $ classify n
    where
    matchValue (_, v) = v
    classify n = find (\(m, _) -> n `mod` m == 0) [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
    classify n = find (\(m, _) -> n `mod` m == 0) [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
  2. jbrains revised this gist May 7, 2014. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,9 @@
    -- What trick can I learn to simplify this?
    -- Thanks to @kimwallmark for teaching me `maybe`.
    -- I'd much rather use something like `lookup` instead of this hand-rolled `matchValue`.
    -- (Typing this last sentence gave me an idea.)

    fizzbuzz :: Integer -> String
    fizzbuzz n = case (classifyingDivisor n) of
    Just divisor -> case (lookup divisor classifications) of
    Just answer -> answer
    Nothing -> "error"
    Nothing -> show n
    fizzbuzz n = maybe (show n) matchValue $ classify n
    where
    classifyingDivisor = \n -> (find (\m -> n `mod` m == 0) [15, 5, 3])
    classifications = [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
    matchValue (_, v) = v
    classify n = find (\(m, _) -> n `mod` m == 0) [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
  3. jbrains revised this gist May 7, 2014. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    -- What trick can I learn to simplify this?

    fizzbuzz :: Integer -> String
    fizzbuzz n = case (classifyingDivisor n) of
    Just 15 -> "Fizzbuzz"
    Just 5 -> "Buzz"
    Just 3 -> "Fizz"
    Just divisor -> case (lookup divisor classifications) of
    Just answer -> answer
    Nothing -> "error"
    Nothing -> show n
    where classifyingDivisor = \n -> (find (\m -> n `mod` m == 0) [15, 5, 3])
    where
    classifyingDivisor = \n -> (find (\m -> n `mod` m == 0) [15, 5, 3])
    classifications = [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
  4. jbrains revised this gist May 7, 2014. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    -- A little clever, but still easy enough to follow, I think.

    fizzbuzz :: Integer -> String
    fizzbuzz n = case (find (\m -> n `mod` m == 0) [15, 5, 3]) of
    fizzbuzz n = case (classifyingDivisor n) of
    Just 15 -> "Fizzbuzz"
    Just 5 -> "Buzz"
    Just 3 -> "Fizz"
    Nothing -> show n
    Nothing -> show n
    where classifyingDivisor = \n -> (find (\m -> n `mod` m == 0) [15, 5, 3])
  5. jbrains revised this gist May 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    -- A little clever, but still easy toenough to follow, I think.
    -- A little clever, but still easy enough to follow, I think.

    fizzbuzz :: Integer -> String
    fizzbuzz n = case (find (\m -> n `mod` m == 0) [15, 5, 3]) of
  6. jbrains revised this gist May 7, 2014. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,8 @@
    -- Feels like there's a shorthand for the duplicated `mod` expressions.
    -- A little clever, but still easy toenough to follow, I think.

    fizzBuzz x
    | (x `mod` 3) == 0 && (x `mod` 5) == 0 = "FizzBuzz"
    | (x `mod` 3) == 0 = "Fizz"
    | (x `mod` 5) == 0 = "Buzz"
    | otherwise = show x

    -- Extracting the mod expressions in any way I know how (so far) leads to more complication.
    fizzbuzz :: Integer -> String
    fizzbuzz n = case (find (\m -> n `mod` m == 0) [15, 5, 3]) of
    Just 15 -> "Fizzbuzz"
    Just 5 -> "Buzz"
    Just 3 -> "Fizz"
    Nothing -> show n
  7. @srbaker srbaker revised this gist May 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    -- Feels like there's a shorthand for the duplicated `mod` expressions.

    fizzBuzz x
    | (x `mod` 3) == 0 && (x `mod` 5) == 0 = "FizzBuzz"
    | (x `mod` 3) == 0 = "Fizz"
    | (x `mod` 5) == 0 = "Buzz"
    | (x `mod` 3) == 0 && (x `mod` 5) == 0 = "FizzBuzz"
    | otherwise = show x

    -- Extracting the mod expressions in any way I know how (so far) leads to more complication.
  8. @srbaker srbaker created this gist May 7, 2014.
    9 changes: 9 additions & 0 deletions gistfile1.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    -- Feels like there's a shorthand for the duplicated `mod` expressions.

    fizzBuzz x
    | (x `mod` 3) == 0 = "Fizz"
    | (x `mod` 5) == 0 = "Buzz"
    | (x `mod` 3) == 0 && (x `mod` 5) == 0 = "FizzBuzz"
    | otherwise = show x

    -- Extracting the mod expressions in any way I know how (so far) leads to more complication.