简体   繁体   English

Haskell字符串列表到Int列表

[英]Haskell String list to Int list

I have a list of strings and I want to read the strings one by one and convert it into a list of ints, is there a way to convert each character into a new list? 我有一个字符串列表,我想逐一读取字符串并将其转换为整数列表,有没有办法将每个字符转换为新列表?

["123","346","789"] to [[1,2,3],[4,5,6],[7,8,9]]

stringToInt :: [String] -> [[Int]]

One of the pillars of functional programming is map , which has the following signature: 函数式编程的支柱之一是map ,它具有以下特征:

map :: (a -> b) -> [a] -> [b]

In other words, it takes a function that expects a value of type a and returns a value of type b (NB these could be the same type, but don't have to be) along with a list of a values, and kicks you back a list of b values. 换句话说,它接受一个期望值为a类型的值并返回类型b的值的函数(注意,这些值可以是同一类型,但不一定是),并带有a值列表,然后踢您返回b值的列表。 Take this example: 举个例子:

double :: Int -> Int
double = (*2)
{- equivalently written as:
   double x = x * 2 -}

as = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
bs = map double as

In this example, map is specialized where a ~ Int and b ~ Int , resolving to 在此示例中,map专用于a ~ Intb ~ Int ,解析为

map :: (Int -> Int) -> [Int] -> [Int]

and bs is then [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] . 然后bs[2, 4, 6, 8, 10, 12, 14, 16, 18, 20] bs [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]


Why this long primer on map ? 为什么在map上这么长的底漆? Because it's the framework to so many answers in functional programming, including your question. 因为它是函数式编程中包括答案在内的许多答案的框架。 Remember that String is just a type synonym for [Char] , so you're trying to go from [[Char]] -> [[Int]] . 请记住, String只是[Char]的类型同义词,因此您尝试从[[Char]] -> [[Int]]转到。 Gee, that sure looks like the last two terms of the type signature for map, doesn't it? e,肯定看起来像map的类型签名的最后两个词,不是吗? Let's specialize map to operate in these terms. 让我们专门针对这些术语进行操作的map

map :: ([Char] -> [Int]) -> [[Char]] -> [[Int]]

But wait, the function that map is expecting also looks like the result of a mapping. 但是等等, map期望的功能看起来像映射的结果。 Let's write that, too. 让我们也这样写。

map :: (Char -> Int) -> [Char] -> [Int]

So what we want is a double map, applied to some function f such that: 因此,我们想要的是一个双重映射,它应用于某些函数f ,使得:

map (map f) :: [[Char]] -> [[Int]]
{- more idiomatically written as
   (map . map) f :: [[Char]] -> [[Int]] -}

This means we need an f :: Char -> Int -- some function that goes from single characters to integers. 这意味着我们需要一个f :: Char -> Int -一些从单个字符到整数的函数。 There's a reasonably small number of defined inputs for that operation, so I'd just write it. 该操作的定义输入的数量相当少,所以我只写它。

digitToInt :: Char -> Int
digitToInt '0' = 0
digitToInt '1' = 1
digitToInt '2' = 2
digitToInt '3' = 3
digitToInt '4' = 4
digitToInt '5' = 5
digitToInt '6' = 6
digitToInt '7' = 7
digitToInt '8' = 8
digitToInt '9' = 9
digitToInt 'a' = 10
digitToInt 'A' = 10
digitToInt 'b' = 11
digitToInt 'B' = 11
digitToInt 'c' = 12
digitToInt 'C' = 12
digitToInt 'd' = 13
digitToInt 'D' = 13
digitToInt 'e' = 14
digitToInt 'E' = 14
digitToInt 'f' = 15
digitToInt 'F' = 15
digitToInt _   = error "Invalid digit"

but NB that this function comes standard in Data.Char 但是请注意,此功能是Data.Char标准Data.Char

import Data.Char (digitToInt)

Your result then is: 那么您的结果是:

result = (map.map) digitToInt ["123","346","789"]
import Data.List (intersperse)

f :: String :: [Int]
f stringnum = read $ "[" ++ intersperse  ',' stringnum ++ "]" :: [Int]

>>> map f ["123", "456"]
[[1,2,3],[4,5,6]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM