# Difference between revisions of "Haskell Quiz/FizzBuzz/Solution Syzygies"

From HaskellWiki

< Haskell Quiz | FizzBuzz

Tomjaguarpaw (talk | contribs) (Remove old, not very useful page) |
m (Reverted edits by Tomjaguarpaw (talk) to last revision by Syzygies) |
||

Line 1: | Line 1: | ||

+ | Fizz comes before Buzz comes before an integer. Fizz and Buzz stick to each other, but hide integers. |
||

+ | |||

+ | The lists for Fizz and Buzz are infinite, but zipping together with a finite list of integers, the result is finite. |
||

+ | |||

+ | <haskell> |
||

+ | module Main where |
||

+ | |||

+ | main :: IO () |
||

+ | main = mapM_ putStrLn $ zipWith3 join (loop 3 "Fizz") (loop 5 "Buzz") [1..100] |
||

+ | where |
||

+ | xor s t = if null s then t else s |
||

+ | loop n s = cycle $ replicate (n-1) [] ++ [s] |
||

+ | join s t n = xor (s ++ t) (show n) |
||

+ | </haskell> |
||

+ | |||

+ | If one has enabled all warnings as errors, then the integers need an explicit type, as shown below. |
||

+ | |||

+ | The hiding logic can also be implemented by filtering for the first non-null element of a list: |
||

+ | |||

+ | <haskell> |
||

+ | module Main where |
||

+ | module Main where |
||

+ | |||

+ | main :: IO () |
||

+ | main = sequence_ $ zipWith3 join (loop 3 "Fizz") (loop 5 "Buzz") [1..100 :: Int] |
||

+ | where |
||

+ | loop n s = cycle $ replicate (n-1) "" ++ [s] |
||

+ | join s t n = putStrLn . head $ filter (not . null) [s ++ t, show n] |
||

+ | </haskell> |

## Latest revision as of 15:19, 6 February 2021

Fizz comes before Buzz comes before an integer. Fizz and Buzz stick to each other, but hide integers.

The lists for Fizz and Buzz are infinite, but zipping together with a finite list of integers, the result is finite.

```
module Main where
main :: IO ()
main = mapM_ putStrLn $ zipWith3 join (loop 3 "Fizz") (loop 5 "Buzz") [1..100]
where
xor s t = if null s then t else s
loop n s = cycle $ replicate (n-1) [] ++ [s]
join s t n = xor (s ++ t) (show n)
```

If one has enabled all warnings as errors, then the integers need an explicit type, as shown below.

The hiding logic can also be implemented by filtering for the first non-null element of a list:

```
module Main where
module Main where
main :: IO ()
main = sequence_ $ zipWith3 join (loop 3 "Fizz") (loop 5 "Buzz") [1..100 :: Int]
where
loop n s = cycle $ replicate (n-1) "" ++ [s]
join s t n = putStrLn . head $ filter (not . null) [s ++ t, show n]
```