7

我在 python 挑战中玩链表问题,需要查询下一个值(猜它是 Int)。

我创建函数来获取下一个值,如下所示

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" 

getNext :: Int -> IO Int
getNext x = do
    rsp <- simpleHTTP (getRequest $ url ++ show x)
    bdy <- getResponseBody rsp
    let num = last $ splitWhen (==' ') bdy
    return (read num::Int)

它工作正常(在ghci中)

> getNext 12345
44827
> getNext 44827
45439

虽然我想反复调用getNext 直到找到答案,但我认为我应该像在非单子世界中那样保留历史记录,这样我就可以从最后一个值继续,以防万一发生故障。

> let nX x = x + 3
> :t nX
nX :: Num a => a -> a
> take 10 $ iterate nX 1
[1,4,7,10,13,16,19,22,25,28]

我认为它应该是iterateM_从 Control.Monad.Loops 中找到的迭代的单子提升版本,但它没有按我预期的那样工作。没有显示(我认为 _ 后缀意味着丢弃结果但没有iterateM

> :t iterate
iterate :: (a -> a) -> a -> [a]
> :t iterateM_
iterateM_ :: Monad m => (a -> m a) -> a -> m b

问题是我怎样才能像在非单子迭代中那样获得 [Int] 。我想我想要一个返回的函数IO [Int]能够像这样在我的代码中提取和过滤/处理

main = do
    i <- getAllList
    let answer = last i -- or could be a repeated converged value, don't know yet
    putStrLn (show answer)

getAllList :: IO [Int]
4

2 回答 2

7

如果您希望您的函数提前终止,而不是返回一个无限的结果列表,您将需要使用unfoldrM而不是 iterateM. 这可以通过以下方式完成:

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" 


start = 12345
stop  = 10000

shouldStop :: Int -> Bool
shouldStop x = x == stop

getNext :: Int -> IO (Maybe (Int, Int))
getNext prev
    | shouldStop prev = return Nothing
    | otherwise       = do
        rsp <- simpleHTTP (getRequest $ url ++ show prev)
        bdy <- getResponseBody rsp
        let num = read $ last $ splitWhen (==' ') bdy :: Int
        print (prev, num)
        return $ Just (num, num)

getAllList :: IO [Int]
getAllList = unfoldrM getNext start

这将允许您定义一个停止条件,以便循环可以终止,但在满足终止条件之前您不会收到返回的结果。

unfoldrM函数可以在monad-loops包中找到,但最新版本一直在重复使用原始种子,而不是生成器函数产生的种子(我相信这已经修复但没有上传到 Hackage)。这是unfoldrM您想要的版本。

-- |See 'Data.List.unfoldr'.  This is a monad-friendly version of that.
unfoldrM :: (Monad m) => (a -> m (Maybe (b,a))) -> a -> m [b]
unfoldrM = unfoldrM'

-- |See 'Data.List.unfoldr'.  This is a monad-friendly version of that, with a
-- twist.  Rather than returning a list, it returns any MonadPlus type of your
-- choice.
unfoldrM' :: (Monad m, MonadPlus f) => (a -> m (Maybe (b,a))) -> a -> m (f b)
unfoldrM' f z = go z
    where go z = do
            x <- f z
            case x of
                Nothing         -> return mzero
                Just (x, z)     -> do
                        xs <- go z
                        return (return x `mplus` xs)

这就是您可以使用Pipes的方式,这将允许您将处理作为结果流进行,而无需求助于惰性 I/O。

import Network.HTTP
import Control.Monad
import Data.List.Split
import Control.Monad
import Control.Proxy

url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="

grabber :: (Proxy p) => Int -> () -> Producer p String IO ()
grabber start () = runIdentityP $ loop $ show start where
    loop x = do
        -- Grab the next value
        x' <- lift $ getNext x
        -- Send it down stream
        respond x'
        -- Keep grabbing
        loop x'

-- Just prints the values recieved from up stream
printer :: (Proxy p, Show a) => () -> Consumer p a IO r
printer () = runIdentityP $ forever $ do
    a <- request ()  -- Consume a value
    lift $ putStrLn $ "Received a value: " ++ show a

getNext :: String -> IO String
getNext prev = do
    rsp <- simpleHTTP (getRequest $ url ++ prev)
    bdy <- getResponseBody rsp
    let num  = last $ splitWhen (== ' ') bdy
    return num

main = runProxy $ grabber start >-> printer
于 2013-02-06T15:22:47.660 回答
1

所以你想要的基本上是

iterateM :: Monad m => (a -> m a) -> a -> m [a]
iterateM action a = do
   a' <- action a
   liftM (a':) $ iterateM action a'

问题是这并不像人们预期的那样懒惰地工作:由于单子绑定是严格的,即使您只想评估有限数量的as,您也会陷入无限循环。

于 2013-02-06T15:08:40.697 回答