问题描述
如果您摆脱了 - 可以很快做到这一点
Prelude>(map(map read)。map words。lines $('a', 3)('b',4)\\\
('c',5)):: [[(Char,Int)]]
[[('a',3),'''或者将它定义为一个函数,4)],[('c',5)]]
genericReadLines :: Read a => String - > [[a]]
genericReadLines = map(map阅读)。map words。lines
你可以这样使用:
* Main>(genericReadLines('a',3)('b',4)\\\
('c',5)):: [[(Char,Int)]]
[[('a',3),('b',4)],[('c',5)]]
但您可能会发现它更容易做到
readCharInts :: String - > [[(Char,Int)]]
readCharInts = genericReadLines
$ b $ readInts :: String - > [[Int]]
readInts = genericReadLines
$ b $ p
$ b pre $ $ $ $ $ $ $ $ *主> readInts1 2 3\\\
4 5 6\\\
7 8 9
[[1,2,3],[4,5,6],[7,8,9]]
*主> readCharInts('a',3)('b',4)\\\
('c',5)
[[('a',3),('b',4)], [('c',5)]]
但是如何保持 - ?您必须使用Maybe数据类型来表示对列表中的某些点没有值;我们可以使用 - 作为 Nothing 和 a 的缩写。 的简写。
阅读'::阅读a => ;字符串 - >也许一个
读' - =没有
读'xs = Just(读xs)
我应该警告你,如果你的数据可能是' - ',那么代码是脆弱的,但也许它不能。
genericMaybeReadLines :: Read a =>字符串 - > [[也许a]]
genericMaybeReadLines =地图(地图阅读')。地图字。行
然后我们可以有
readMaybeCharInts :: String - > [[Maybe(Char,Int)]]
readMaybeCharInts = genericMaybeReadLines
readMaybeInts :: String - > [[Maybe Int]]
readMaybeInts = genericMaybeReadLines
现在我们可以做
* Main> readMaybeCharInts('a',3)('b',4)\\\
-('c',5)
[[Just('a',3),Just('b',4 )],[Nothing,Just('c',5)]]
* Main> readMaybeInts2 3 -\
4 - 2
[[Just 2,Just 3,Nothing],[Just 4,Nothing,Just 2]]
I am trying to write instance of Read class to read an input as string as follow:
"1 3 - - 2 3 - - 5"Convert it into [[Maybe Int]]
"-" will be translated into Nothing"3" will be Just 3
"('a',3) - ('b',7) ('c',5) ('e',0) - ('d',9) - ('h',8)"Convert it into [[Maybe (Char,Int)]]
"-" will be translated into Nothing"('a',3)" will be Just ('a',3)
I tried to write them by handling with List of Char, but it takes a lot of work. Do you have any suggestion? Sorry, I am quite new to Haskell, so I ask you that question. :(
解决方案If you got rid of the - entries, you could do this very quickly as
Prelude> (map (map read) . map words. lines $ "('a',3) ('b',4)\n('c',5)")::[[(Char,Int)]] [[('a',3),('b',4)],[('c',5)]]Or define it as a function
genericReadLines :: Read a => String -> [[a]] genericReadLines = map (map read) . map words. linesWhich you can use thus:
*Main> (genericReadLines "('a',3) ('b',4)\n('c',5)")::[[(Char,Int)]] [[('a',3),('b',4)],[('c',5)]]but you may find it easier to do
readCharInts :: String -> [[(Char,Int)]] readCharInts = genericReadLines readInts :: String -> [[Int]] readInts = genericReadLinesSo you can just type
*Main> readInts "1 2 3\n4 5 6\n7 8 9" [[1,2,3],[4,5,6],[7,8,9]] *Main> readCharInts "('a',3) ('b',4)\n('c',5)" [[('a',3),('b',4)],[('c',5)]]But what about keeping the -? You'll have to use a Maybe data type, to represent not having a value for certain points in your list; we can use - as shorthand for Nothing and a as shorthand for Just a.
read' :: Read a => String -> Maybe a read' "-" = Nothing read' xs = Just (read xs)I should warn you that that code is fragile if your data could possibly be '-', but perhaps it can't.
genericMaybeReadLines :: Read a => String -> [[Maybe a]] genericMaybeReadLines = map (map read') . map words. linesThen we can have
readMaybeCharInts :: String -> [[Maybe (Char,Int)]] readMaybeCharInts = genericMaybeReadLines readMaybeInts :: String -> [[Maybe Int]] readMaybeInts = genericMaybeReadLinesSo now we can do
*Main> readMaybeCharInts "('a',3) ('b',4)\n- ('c',5)" [[Just ('a',3),Just ('b',4)],[Nothing,Just ('c',5)]] *Main> readMaybeInts "2 3 -\n4 - 2" [[Just 2,Just 3,Nothing],[Just 4,Nothing,Just 2]]
这篇关于读取一个字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!