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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:24:18+00:00 2026-06-17T14:24:18+00:00

I want to write a function that can list a directory recursively in breadth-first

  • 0

I want to write a function that can list a directory recursively in breadth-first in Haskell.
As you can see I need a function that can convert a (a -> IO b) to a IO (a->b). Simple as it seems, I can’t make it. And I want to know how to do or whether it is possible.

dirElem :: FilePath -> IO [FilePath]
dirElem dirPath = do
  getDirectoryContents'' <- theConvert getDirectoryContents'
  return $ takeWhile (not.null) $ iterate (concatMap getDirectoryContents'') [dirPath] where
    getDirectoryContents' dirPath = do
      isDir <- do doesDirectoryExist dirPath
      if isDir then dirContent else return [] where
        dirContent = do
          contents <- getDirectoryContents dirPath
          return.(map (dirElem</>)).tail.tail contents
    theConvert :: (a -> IO b) -> IO (a -> b)
    theConvert = ??????????
  • 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-17T14:24:19+00:00Added an answer on June 17, 2026 at 2:24 pm

    This cannot be done. The reason is that the function can use its argument of type a to determine what IO action is executed. Consider

    action :: Bool -> IO String
    action True  = putStrLn "Enter something:" >> getLine
    action False = exitFailure
    

    Now if you’d convert it somehow to IO (Bool -> String) and evaluate this action, what should happen? There is no solution. We cannot decide if we should read a string or exit, because we don’t know the Bool argument yet (and we may never know it, if the resulting function isn’t called on an argument).

    John’s answer is a bad idea. It simply lets the IO action escape into pure computations, which will make your life miserable and you’ll lose Haskell’s referential transparency! For example running:

    main = unsafe action >> return ()
    

    will do nothing even though the IO action was called. Moreover, if we modify it a bit:

    main = do
       f <- unsafe action
       putStrLn "The action has been called, calling its pure output function."
       putStrLn $ "The result is: " ++ f True
    

    you’ll see that the action that asks for an input is executed in a pure computation, inside calling f. You’ll have no guarantee when (if at all) the action is executed!

    Edit: As others pointed out, it isn’t specific just to IO. For example, if the monad were Maybe, you couldn’t implement (a -> Maybe b) -> Maybe (a -> b). Or for Either, you couldn’t implement (a -> Either c b) -> Either c (a -> b). The key is always that for a -> m b we can choose different effects depending on a, while in m (a -> b) the effect must be fixed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a C++ function that can give me a list of
I want to write a function that takes a list of numbers and returns
I want to write a function that flattens a List. object Flat { def
I want to write a function that calls another function with its arguments. See
In Javascript, I want to write a function that returns a list of all
I want to write a function that accepts a parameter which can be either
I want to write a function that identifies all the links on a particular
i want to write a function that prints multi-dimensional objects which are text (or
I want to write 'twice' function that takes a function and an argument and
I want to write a function that read line by line from a socket

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.