Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:05:23+00:00 2026-06-18T12:05:23+00:00

I am playing with linkedlist problem in python challenge that require querying a next

  • 0

I am playing with linkedlist problem in python challenge that require querying a next value (guess it be Int).

I create function for get the next value as follows

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)

and it work fine (in ghci)

> getNext 12345
44827
> getNext 44827
45439

While I suppose to repeatedly call getNext until I found the answer, I think I should keep the history like I can do in non-monadic world so I can continue from the last value in case something fail.

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

I think it should be a monadic lifted version of iterate and found iterateM_ from Control.Monad.Loops but it didn’t work as I expected. There is nothing shown (I think _ suffix mean discard the result but there is no iterateM)

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

Question is how can I get [Int] as in non-monadic iteration. I think I want a function that return IO [Int] to be able to pull-out and filter/process in my code like this

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:05:25+00:00Added an answer on June 18, 2026 at 12:05 pm

    If you want your function to terminate early, rather than give back an
    infinite list of results, you will want to use unfoldrM rather than
    iterateM. This can be done with something like the following:

    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
    

    This will allow you to define a stopping criteria so that the loop can
    terminate, but you will not receive results back until the termination
    criteria has been met.

    The unfoldrM function can be found in the monad-loops package, but the
    latest version keeps reusing the original seed rather than the one produced by
    the generator function (I believe this has been fixed but not uploaded to
    Hackage). This is the version of unfoldrM that you would want.

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

    This is how you might go about this using Pipes, which will allow you to
    do the processing as a stream of results without resorting to lazy 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Playing with Disqus for commenting, but the problem is that we have a Silverlight
Playing around with Python - tkInter - Entry widget - when I use validatecommand
Playing with jquery for the first time, and I'm trying to get a simple
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
been playing around with ResolveClientUrl(~/Confirmation.aspx) and other methods.. I am tryin go get the
playing around with a property file i figured that there seems to be a
Playing with json in Python's STL and came up with this.. import json as
Playing around with http://tympanus.net/Tutorials/CufonizedFlyOutMenu/ I dropped the fly-in descriptions and now trying to get
Playing around a bit more with jqTouch and am running into a problem with
Whilst playing around with an nhibernate mapping, I noticed that a property setter I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.