1
\$\begingroup\$

I am trying to pick up Haskell!

But I feel like my code is a bit painful to write and read.

Do you have any pointers on how to improve its readability / style?

main = do
 contents <- fmap tail . fmap lines . readFile $ "testInput"
 let input = map pair_to_tuple .(map (map read)) . map words $ contents :: [(Int,Int)]
 let result = map (uncurry waffles) $ input
 let decorated_result = ["Case #" ++ show i ++ ": " ++ show s | (i,s) <- zip [1..length result] result] 
 writeFile "output.txt" $ unlines $ decorated_result
waffles row col = (row - 1)*(col - 1)
pair_to_tuple [a,b] = (a,b)

This script reads a file whose first line is a title, then every line is a couple of numbers. Then it drops the first line, and processes every pair of numbers using the function waffles, then it writes it back to an output file.

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Apr 25, 2018 at 17:32
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$
contents <- fmap tail . fmap lines . readFile $ "testInput"

can be written as

contents <- (tail . lines) <$> readFile "testInput"

Here we take advantage of the law fmap f . fmap g == fmap (f . g). <$> is an infix version of fmap. $ is extraneous here.


let input = map pair_to_tuple .(map (map read)) . map words $ contents :: [(Int,Int)]

can be written as

let input = map (pair_to_tuple . (map read) . words) contents :: [(Int,Int)]

For lists map = fmap, so map f . map g == map (f . g) also holds.


In

let result = map (uncurry waffles) $ input

the $ sign is also extraneous.


let decorated_result = ["Case #" ++ show i ++ ": " ++ show s | (i,s) <- zip [1..length result] result]

You can exploit Haskell's lazy evaluation and create infinite list to zip with:

let decorated_result = ["Case #" ++ show i ++ ": " ++ show s | (i,s) <- zip [1..] result]

Otherwise I'd say the code is fine.

answered Apr 25, 2018 at 18:32
\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your review! Why do you think I may have a typo on [1..length result]? The code certainly compiles and works as expected. \$\endgroup\$ Commented Apr 25, 2018 at 20:02
  • 1
    \$\begingroup\$ @Jsevillamol Oh, right, stupid me. Updated my answer. \$\endgroup\$ Commented Apr 26, 2018 at 4:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.