读取一个字符串输入 [英] Read a string input

查看:120
本文介绍了读取一个字符串输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 1 3  -  $ 

我试图编写Read类的实例来读取输入,如下所示: b $ b - 2 3
- - 5

将其转换为 [[Maybe Int]]



- 会被翻译成 Nothing
3将是只是3

  ('a',3) - ('b',7)
('c',5)('e',0) -
('d',9) - ('h' ,8)

将其转换为 [[Maybe(Char,Int) ]]



- 将被转换为Nothing
('a',3)将会被变为 Just('a',3)



我试图通过使用List of Char来编写它们,但它需要很多工作的。你有什么建议吗?对不起,我对Haskell很新,所以我问你这个问题。 :(


解决方案

如果您摆脱了 - 可以很快做到这一点

  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. lines

Which 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 = genericReadLines

So 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. lines

Then we can have

readMaybeCharInts :: String -> [[Maybe (Char,Int)]]
readMaybeCharInts = genericMaybeReadLines

readMaybeInts :: String -> [[Maybe Int]]
readMaybeInts = genericMaybeReadLines

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

这篇关于读取一个字符串输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆