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 6730065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:50+00:00 2026-05-26T10:21:50+00:00

just started learning haskell – love it after a week. At the moment going

  • 0

just started learning haskell – love it after a week. At the moment going through the monads pain, not there yet but hopefully it will click.

I am trying to put together a function similar to pythons walk() but simpler.
Given a path I want to generate list of tuples. A tuple for each sub directory (lets just assume that there will be only directories). The tuple would contain path of the directory as its first element and list of files the directory contains as the second element.

I don’t know if I explained it correctly but here is the code:

walkDir :: String -> IO [IO (FilePath, [FilePath])]
walkDir path = do
  dir <- getDirectoryContents path  
  let nd = [x | x <- dir, notElem x [".",".."]]
  return (map getcont nd)
    where
      getcont path = do
      cont <- getDirectoryContents path
      return (path,cont)

My concern is IO inside IO and how to deal with it?? Is it possible to cancel them out?
Is it possible to unwrap at least the internal IO? Is this kind of return normal?

I can not even print this kind of return. Do I have to create an instance of show for this to be printed correctly?

There most likely exist a similar function in some haskell library but this is for educational purpose. I want to learn. So any suggestions welcome.

Thank you very much.

  • 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-05-26T10:21:50+00:00Added an answer on May 26, 2026 at 10:21 am

    Let’s look at the types.

    map :: (a -> b) -> [a] -> [b]
    getCont :: FilePath -> IO (FilePath, [FilePath])
    nd :: [FilePath]
    
    map getCont nd :: [IO (FilePath, FilePath)]
    

    Now, at this point, the structure looks inside-out. We have [IO a] but we want IO [a]. Stop! Hoogle time. Generalizing for any ol’ monad, we hoogle [m a] -> m [a]. Lo and behold, sequence has that precise type signature. So instead of return :: a -> m a you should use sequence :: [m a] -> m [a], like this:

    sequence (map getCont nd)
    

    Then you’ll be all set. Notice that this is essentially identical to Kurt S’s solution, because

    mapM f xs = sequence (map f xs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner in Haskell so please bear with me. (Just started learning
I just started learning about Kinect through some quick start videos and was trying
Just started re-learning Haskell (did it at uni but forgot most of it) and
I just started learning haskell, and I tried to implement a simple function to
Just started learning OpenGL and couldn't find a decent tutorial to get me going.
I just started learning about pointers in C++, and I'm not very sure on
I have just recently started learning Haskell and I am having a lot of
I've just started learning about parsing, and I wrote this simple parser in Haskell
I just started learning php last week and I am trying to populate values
I just started learning Haskell and I ran into a problem in 2-adic type

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.