Skip to main content
Citation
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Credit is due: I saw this originally on /r/haskell but the user deleted his or her comment so I'm unsure who the attribution belongs to/r/haskell.

Credit is due: I saw this originally on /r/haskell but the user deleted his or her comment so I'm unsure who the attribution belongs to.

Credit is due: I saw this originally on /r/haskell.

added 112 characters in body
Source Link
hao
  • 486
  • 2
  • 8
handleFizzBuzz :: String -> String
handleFizzBuzz =
    map $ unwords . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . map handleFizzBuzz . lines $ file
import Data.Monoid
import Data.Maybe
import Control.Monad
import System.Environment

fizzBuzzSingle :: Int -> Int -> Int -> Maybe String
fizzBuzzSingle f b n =
    ("F" <$ guard (mod n f == 0)) <> ("B" <$ guard (mod n b == 0))

fizzBuzz :: [Int] -> [String]
fizzBuzz [f, b, end] =
    map (fromMaybe . show <*> fizzBuzzSingle f b) [1 .. end]

main :: IO ()
main = do
    args <- getArgs
    file <- readFile (head args)
    (putStrLn . unlines . map (unwords . fizzBuzz . map read . words) . lines) file
handleFizzBuzz :: String -> String
handleFizzBuzz =
    map $ unwords . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . handleFizzBuzz . lines $ file
fizzBuzzSingle :: Int -> Int -> Int -> Maybe String
fizzBuzzSingle f b n =
    ("F" <$ guard (mod n f == 0)) <> ("B" <$ guard (mod n b == 0))

fizzBuzz :: [Int] -> [String]
fizzBuzz [f, b, end] =
    map (fromMaybe . show <*> fizzBuzzSingle f b) [1 .. end]

main :: IO ()
main = do
    args <- getArgs
    file <- readFile (head args)
    (putStrLn . unlines . map (unwords . fizzBuzz . map read . words) . lines) file
handleFizzBuzz :: String -> String
handleFizzBuzz =
    map $ unwords . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . map handleFizzBuzz . lines $ file
import Data.Monoid
import Data.Maybe
import Control.Monad
import System.Environment

fizzBuzzSingle :: Int -> Int -> Int -> Maybe String
fizzBuzzSingle f b n =
    ("F" <$ guard (mod n f == 0)) <> ("B" <$ guard (mod n b == 0))

fizzBuzz :: [Int] -> [String]
fizzBuzz [f, b, end] =
    map (fromMaybe . show <*> fizzBuzzSingle f b) [1 .. end]

main :: IO ()
main = do
    args <- getArgs
    file <- readFile (head args)
    (putStrLn . unlines . map (unwords . fizzBuzz . map read . words) . lines) file
Edited code to match advice
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Some functions in the Prelude can be of help. One is map, which allows you to write a simpler handleFizzBuzz. Another is unwords, which is equivalent to your concatOutput.

handleFizzBuzz :: String -> String
handleFizzBuzz =
    concatOutputmap $ unwords . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . handleFizzBuzz . lines $ file

Another is unwords, which is equivalent to your concatOutput.

Some functions in the Prelude can be of help. One is map, which allows you to write a simpler handleFizzBuzz.

handleFizzBuzz :: String -> String
handleFizzBuzz =
    concatOutput . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . handleFizzBuzz . lines $ file

Another is unwords, which is equivalent to your concatOutput.

Some functions in the Prelude can be of help. One is map, which allows you to write a simpler handleFizzBuzz. Another is unwords, which is equivalent to your concatOutput.

handleFizzBuzz :: String -> String
handleFizzBuzz =
    map $ unwords . fizzBuzz . convertInputLine

main :: IO ()
main = do
    args <- getArgs
    let path = args !! 0
    file <- readFile path
    putStrLn . unlines . handleFizzBuzz . lines $ file
Source Link
hao
  • 486
  • 2
  • 8
Loading