Home > Back-end >  Casting list of lists in Haskell
Casting list of lists in Haskell

Time:12-13

I have a beginner Haskell question.

I'm using Advent of Code this year to learn Haskell. Working through the first problem, I need to cast strings to integers.

This is my code:

import Data.List.Split
import System.IO

main = do
    input <- getContents
    let bags = splitWhen (=="") $ lines input
    let bagsInteger = map (\arr -> map (\x -> read x :: Integer)) bags :: [[Integer]]
    let totals = map (sum) bagsInteger
    putStrLn $ show $ maximum totals

When running ghc, I get

azl@Alains-MacBook-Air aoc-2022 % ghc 1.hs
Loaded package environment from /Users/azl/.ghc/aarch64-darwin-9.2.5/environments/default
[1 of 1] Compiling Main             ( 1.hs, 1.o )

1.hs:7:36: error:
    • Couldn't match expected type: [Integer]
                  with actual type: [String] -> [Integer]
    • Probable cause: ‘map’ is applied to too few arguments
      In the expression: map (\ x -> read x :: Integer)
      In the first argument of ‘map’, namely
        ‘(\ arr -> map (\ x -> read x :: Integer))’
      In the expression:
          map (\ arr -> map (\ x -> read x :: Integer)) bags :: [[Integer]]
  |
7 |     let bagsInteger = map (\arr -> map (\x -> read x :: Integer)) bags :: [[Integer]]

Any help is appreciated!

CodePudding user response:

You forgot arr!

map (\arr -> map (...) arr) bags
--                     ^^^
  • Related