Convert a String list to an Int list

前端 未结 3 1476
情歌与酒
情歌与酒 2020-12-16 12:06

I\'ve got a list of strings, is it possible to convert it to an list of ints?
E.g.:

[\"1\",\"2\"] -> [1,2]
3条回答
  •  失恋的感觉
    2020-12-16 12:15

    This fails:

    map read ["1","2"]
    [*Exception: Prelude.read: no parse
    

    The way to do it is :

     map (read::String->Int) ["1","2"]
     [1,2]
     :: [Int]
    

    Outside of GHCI, in a .hs file it would be:

    let intList = map (read::String->Int) ["1","2"]
    

提交回复
热议问题