haskell 列表演员表

pqwbnv8z  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(130)

我有一个初级 haskell 问题。
今年我用代码降临来学习Haskell。在学习first problem时,我需要将字符串转换为整数。
这是我的代码:

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

当运行ghc时,我得到

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]]

任何帮助都是感激不尽的!

xqkwcwgp

xqkwcwgp1#

你忘了arr

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

相关问题