Haskell Io 字符串转换

发布于 2024-11-15 23:30:33 字数 579 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-11-22 23:30:33

试图解读您的想法的无力尝试:

obrob fp :: Integral i, Read i => (i, [[i]])
obrob fp = let xs = readData fp
           in (read $ readData fp !! 0, read $ readData fp !! 2)

我假设您的 do 语句使用的是列表的单子版本......我不太确定。您需要提供有关类型的更多详细信息,例如 readData、fp 等。

A feeble attempt at reading your mind:

obrob fp :: Integral i, Read i => (i, [[i]])
obrob fp = let xs = readData fp
           in (read $ readData fp !! 0, read $ readData fp !! 2)

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.

王权女流氓 2024-11-22 23:30:33

事实上,您无法真正“摆脱”IO。但当你刚接触 Haskell 时,这似乎并不是问题。查看 main 的类型:

main :: IO ()

您的整个程序(或者实际上的任何程序)都是正在评估的大型 IO 操作。我们试图做的是一路上进行大量的纯计算。

怎么样(如果这只会让问题更加混乱,我很抱歉):

-- Simulate your file read operation
readData :: IO [String]
readData = 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]]"]


-- pure function - not IO
someOtherFunction (x, ys) = (x > 0, length ys)


obrob :: IO (Bool, Int)
obrob = do
   -- Pattern match out the 1st and 3rd elements
   (a:_:b:_) <- readData

   -- t is the tuple you're trying to get to
   let t = ((read a) :: Int, (read b) :: [[Int]])
   print t 

   -- And inside this do block, that t is not in IO.
   -- Lets pass it to a pure function:
   let u = someOtherFunction t

   -- Later we have to evaluate to something in IO.
   -- It cannot be escaped.
   return u

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:

main :: IO ()

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):

-- Simulate your file read operation
readData :: IO [String]
readData = 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]]"]


-- pure function - not IO
someOtherFunction (x, ys) = (x > 0, length ys)


obrob :: IO (Bool, Int)
obrob = do
   -- Pattern match out the 1st and 3rd elements
   (a:_:b:_) <- readData

   -- t is the tuple you're trying to get to
   let t = ((read a) :: Int, (read b) :: [[Int]])
   print t 

   -- And inside this do block, that t is not in IO.
   -- Lets pass it to a pure function:
   let u = someOtherFunction t

   -- Later we have to evaluate to something in IO.
   -- It cannot be escaped.
   return u
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文