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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:00:04+00:00 2026-06-17T16:00:04+00:00

I writed a Haskell module to list all the contents of a directory by

  • 0

I writed a Haskell module to list all the contents of a directory by breadth-first order. The below is the source code.

module DirElements (dirElem) where

import System.Directory (getDirectoryContents, doesDirectoryExist)
import System.FilePath ((</>))

dirElem :: FilePath -> IO [[FilePath]]
dirElem dirPath = iterateM (not.null) (concatMapM getDirectoryContents') [dirPath] >>= return.tail

getDirectoryContents' :: FilePath -> IO [FilePath]
getDirectoryContents' dirPath = do
  isDir <- do doesDirectoryExist dirPath
  if isDir then dirContent else return [] where
    dirContent = do
      contents <- getDirectoryContents dirPath
      return.(map (dirPath</>)).tail.tail $ contents

iterateM :: (Monad m) => (a -> Bool) -> (a -> m a) -> a -> m [a]
iterateM fb f x = do --Notice: Due to the the implementation of >>=, iterateM can't be writen like iterate which gives a infinite list and have type of iterateM :: (Monad m) => (a -> Bool) -> (a -> m a) -> a -> m [a]
  if fb x
    then do
      tail <- do {fx <- f x; iterateM fb f fx}
      return (x:tail)
    else return []

concatMapM :: Monad m => (a -> m[b]) -> [a] -> m[b]
concatMapM f list = mapM f list >>= return.concat

It works correct but when performing on a large directory, it will “suspend” for a little while, and spring out all the results.

After a research I find it is the same question with sequence $ map return [1..]::[[Int]] see Why the Haskell sequence function can’t be lazy or why recursive monadic functions can’t be lazy

  • 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-17T16:00:05+00:00Added an answer on June 17, 2026 at 4:00 pm

    I modified the older answer that Davorak linked to to use the new pipes library.

    It uses StateP to keep a queue of untraversed directories so that it can do a breadth first traversal. It uses MaybeP for exiting from the loop, as a convenience.

    import Control.Monad
    import Control.Proxy
    import Control.Proxy.Trans.Maybe
    import Control.Proxy.Trans.State as S
    import Data.Sequence hiding (filter)
    import System.FilePath.Posix
    import System.Directory
    
    getUsefulContents :: FilePath -> IO [FilePath]
    getUsefulContents path
      = fmap (filter (`notElem` [".", ".."])) $ getDirectoryContents path
    
    traverseTree
        :: (Proxy p)
        => FilePath
        -> () -> Producer (MaybeP (StateP (Seq FilePath) p)) FilePath IO r
    traverseTree path () = do
        liftP $ S.modify (|> path)
        forever $ do
            x <- liftP $ S.gets viewl
            case x of
                EmptyL    -> mzero
                file :< s -> do
                    liftP $ S.put s
                    respond file
                    p <- lift $ doesDirectoryExist file
                    when p $ do
                        names <- lift $ getUsefulContents file
                        let namesfull = map (file </>) names
                        liftP $ forM_ namesfull $ \name ->
                            S.modify (|> name)
    

    This defines a breadth-first lazy producer of files. If you hook it up to a printing stage, it will print out the files as it traverses the tree:

    main = runProxy $ evalStateK empty $ runMaybeK $
        traverseTree "/tmp" >-> putStrLnD
    

    Laziness means that if you only demand 3 files, it will only traverse the tree as much as necessary to generate three files, then it will stop:

        main = runProxy $ evalStateK empty $ runMaybeK $
            traverseTree "/tmp" >-> takeB_ 3 >-> putStrLnD
    

    If you want to learn more about the pipes library, then I recommend you read the tutorial.

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

Sidebar

Related Questions

I write the following Haskell code which take a triplet (x,y,z) and a list
Haskell's cabal package manager stores recent package source code in ~/Library/Haskell/repo-cache/*/*.tgz I've found that
I'm trying to write a Haskell function that checks if a list of integers
I'm trying to write some Haskell code in which there are multiple data types,
I want to create 1MB String for benchmark,so I writed code as follow: public
I'm learning Haskell and am trying to write code as fast as I can
I'm trying to write a program in Haskell that gets a list (of integer)
I'm writing a breadth depth-first tree traversal function, and what I want to do
I'm trying to write 'fizzbuzz' in haskell using list comprehensions. Why doesn't the following
I'm new to haskell and I'm attempting to write my first haskell C library.

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.