Search code examples
haskellfizzbuzz

FizzBuzz cleanup


I'm still learning Haskell, and I was wondering if there is a less verbose way to express the below statement using 1 line of code:

map (\x -> (x, (if mod x 3 == 0 then "fizz" else "") ++ 
 if mod x 5 == 0 then "buzz" else "")) [1..100]

Produces: [(1,""),(2,""),(3,"fizz"),(4,""),(5,"buzz"),(6,"fizz"),(7,""),(8,""),(9,"fizz"),(10,"buzz"),(11,""),(12,"fizz"),(13,""),(14,""),(15,"fizzbuzz"),(16,""),(17,""),(18,"fizz"),(19,""),(20,"buzz"),(21,"fizz"),(22,""),(23,""),(24,"fizz"),(25,"buzz"),(26,""),(27,"fizz"),(28,""),(29,""),(30,"fizzbuzz"), etc

It just feels like I'm fighting the syntax more than I should. I've seen other questions for this in Haskell, but I'm looking for the most optimal way to express this in a single statement (trying to understand how to work the syntax better).


Solution

  • If you insist on a one-liner:

    [(x, concat $ ["fizz" | mod x 3 == 0] ++ ["buzz" | mod x 5 == 0]) | x <- [1..100]]