Haskell Io 字符串转换
obrob fp = do
a <- [(!!) readData fp 0]
b <- [(!!) readData fp 2]
return a --(read a :: Int ,read b::[[Int]] )
我从文件中读取数据,我得到
["6",
"",
"[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]"
]
readData 返回此数据。它是 Io 字符串列表
,但现在我想从此列表中获取第一个和第三个元素并返回,
(6,
[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]
)
但不带 Io 类型。我不想一直使用 monad。
obrob fp = do
a <- [(!!) readData fp 0]
b <- [(!!) readData fp 2]
return a --(read a :: Int ,read b::[[Int]] )
I read data from file I get
["6",
"",
"[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]"
]
readData return this. It is Io string list
But now I want to take first and third element from this list and return
(6,
[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]
)
with out Io type. I don't want to use monad all time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试图解读您的想法的无力尝试:
我假设您的 do 语句使用的是列表的单子版本......我不太确定。您需要提供有关类型的更多详细信息,例如 readData、fp 等。
A feeble attempt at reading your mind:
I'm assuming your do statement was using the monadic version of list... I'm not quite sure. You need to give more details on the types, for example of readData, fp, etc.
事实上,您无法真正“摆脱”IO。但当你刚接触 Haskell 时,这似乎并不是问题。查看 main 的类型:
您的整个程序(或者实际上的任何程序)都是正在评估的大型 IO 操作。我们试图做的是一路上进行大量的纯计算。
怎么样(如果这只会让问题更加混乱,我很抱歉):
The truth is you can't really "get rid" of the IO. But this is not the problem it seems to be when you're new to Haskell. Look at the type of main:
Your whole program - or any program really - is a big IO action thats being evaluated. What we try to do is a lot of pure computation along the way.
How about this (and my apologies if this only confuses the issue more):