我仍在学习Haskell,我想知道是否有一种不太冗长的方法来使用1行代码来表达以下语句:
map (\x -> (x, (if mod x 3 == 0 then "fizz" else "") ++
if mod x 5 == 0 then "buzz" else "")) [1..100]
产生:
[(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")
等感觉就像我在拼写语法方面比应做的更多。我已经在Haskell中看到了其他问题,但是我正在寻找一种最佳的方式来在单个语句中表达这一点(试图了解如何更好地使用语法)。
最佳答案
如果您坚持使用单线:
[(x, concat $ ["fizz" | mod x 3 == 0] ++ ["buzz" | mod x 5 == 0]) | x <- [1..100]]
关于haskell - FizzBuzz清理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9047775/