General FizzBuzz: Difference between revisions

Content added Content deleted
(→‎{{header|R}}: Changed variable name, refactored if statement, and added new solution.)
Line 1,406: Line 1,406:
<lang haskell>type Rule = (Int, String)
<lang haskell>type Rule = (Int, String)


----------------- FIZZETC (USING RULE SET) ---------------
testFizz :: Int -> [String]
testFizz = fizz [(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]


fizz :: [Rule] -> Int -> [String]
fizzEtc :: [Rule] -> Int -> [String]
fizz rules n = foldr nextLine [] [1 .. n]
fizzEtc rules n = foldr nextLine [] [1 .. n]
where
where
nextLine x a =
nextLine x a
(if null noise
| null noise = show x : a
then show x
| otherwise = noise : a
else noise) :
a
where
where
noise = foldl reWrite [] rules
noise = foldl reWrite [] rules
reWrite s (m, k) =
reWrite s (m, k)
s ++
| 0 == rem x m = s <> k
(if rem x m == 0
| otherwise = s

then k

else [])
------------------- TEST OF SAMPLE RULES -----------------

testFizz :: Int -> [String]
testFizz = fizzEtc [(3, "Fizz"), (5, "Buzz"), (7, "Baxx")]


main :: IO ()
main :: IO ()