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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:50:00+00:00 2026-05-27T17:50:00+00:00

I have a Haskell function that operates on a single file to produce a

  • 0

I have a Haskell function that operates on a single file to produce a map. I want to iterate over all files in a directory and apply this function to produce a single map.

I am trying to approach it this way:

perFileFunc :: Int -> FilePath -> IO (Map.Map [Char] Double)

allFilesIn dir =  filter (/= "..")<$>(filter(/= ".")<$>(getDirectoryContents dir)

This gives me a list of all filenames in a directory except . and ..

Now when I try and do

myFunc dir n = map (perFileFunc n) <$> allFilesIn dir

It typechecks but doesn’t do anything. I was expecting a list of maps which I would join using a unionWith (+) perhaps.

This doesn’t seem to be the right way to do this.

  • 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-27T17:50:00+00:00Added an answer on May 27, 2026 at 5:50 pm

    The tricky thing to understand about Haskell is how to recognize and compose IO actions. Let’s look at some type signatures.

    dir :: FilePath
    allFilesIn :: FilePath -> IO [FilePath]
    perFileFunc :: Int -> FilePath -> IO (Map.Map [Char] Double)    
    

    Now then, you said that for myFunc:

    I was expecting a list of maps

    So for that function, you want the type signature

    myFunc :: Int -> FilePath -> [Map.Map String Double]
    

    Of course, the return type can’t be just [Map.Map String Double], because we need to perform some IO in order to evaluate myFunc. So given an Int and a FilePath, we actually want the return type to be an IO action that produces a [Map.Map String Double]:

    myFunc :: Int -> FilePath -> IO [Map.Map String Double]
    

    Now then, let’s look at the IO actions we will be composing to create this function.

    allFilesIn dir :: IO [FilePath]
    perFileFunc n  :: FilePath -> IO (Map.Map String Double)
    

    perFileFunc isn’t actually an IO action, but it is a function that, given a FilePath, produces an IO action. So let’s see…if we run the allFilesIn action, then we can work with that list and run perFileFunc n on each of its elements.

    myFunc dir n = do
      files <- allFilesIn dir
      ???
    

    So what goes in the ??? spot? We have a [FilePath] at our disposal, since we used <- to run the action allFilesIn dir. And we have a function, perFileFunc n :: FilePath -> IO (Map.Map String Double). And we want the result to have the type IO [Map.Map String Double].

    Stop…Hoogle time! Generalizing the components we have (a = FilePath, b = Map.Map String Double), we hoogle for [a] -> (a -> IO b) -> IO [b] (pretending we haven’t seen ehird’s answer yet). Lo and behold, mapM is the magical solution we were looking for! (or forM, which is just flip mapM)

    myFunc dir n = do
      files <- allFilesIn dir
      mapM (perFileFunc n) files
    

    If you desugar this do notation, you’ll find it reduces to ehird’s answer:

    myFunc dir n = allFilesIn dir >>= (\files -> mapM (perFileFunc n) files)
    -- eta reduce (\x -> f x) ==> f
    myFunc dir n = allFilesIn dir >>= mapM (perFileFunc n)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a file called time.hs . It contains a single function that
I am looking for a Haskell function that returns the capturing groups of all
I've started experimenting with Haskell and have a problem. qqq is a function that
I have a haskell function that that calculates the size of the list of
I'm writing an audio program in Haskell using Portaudio. I have a function that
I have a C function that creates a null terminated string and returns a
I know I already have the Haskell Data.ByteString.Lazy function to split a CSV on
I have some Haskell code that does work correctly on an infinite list, but
I have this function in Haskell (I am using the Haskell-SDL library): pixel ::
I have a Haskell Map, containing strings as keys and some lambda functions as

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.