Baranidharan S
Baranidharan S

Reputation: 135

Haskell: Call a function for each list from tuple

I'm trying to solve one of a HackerRank problem, Cats and a Mouse, using Haskell.

I've implemented the logic to solve the problem, though facing some trouble getting the input and parsing to the required shape.

Code:

solve :: [Int] -> String
solve input
    | catA == catB = "Mouse C"
    | catA > catB  = "Cat B"
    | otherwise    = "Cat A"
    where catA = abs $ input !! 0 - input !! 2
          catB = abs $ input !! 1 - input !! 2

main = interact $ unlines . map solve . splitAt 3 . map read . tail . words

I know that splitAt function returns a tuple. So I need help in parsing the tuple here.

I need to call solve function for each list present in the tuple and append the result to output.

Can someone please help me solve this?

Upvotes: 0

Views: 190

Answers (2)

amalloy
amalloy

Reputation: 91857

You don't want splitAt 3, you want Data.List.Split.chunksOf 3. If you don't have Data.List.Split available on hackerrank, you'll need to reimplement chunksOf one way or another.

Upvotes: 0

Robin Zigmond
Robin Zigmond

Reputation: 18249

One simple way to do it is to simply replace your map solve with a simple anonymous function:

main = interact $ unlines . (\(a, b) -> [solve a, solve b]) . splitAt 3 . map read . tail . words

If you dislike anonymous functions, you could always extract it under some name and define it, either as a top level function, or in a where clause.

Upvotes: 1

Related Questions