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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:27:26+00:00 2026-05-11T23:27:26+00:00

I am a Java programmer who learns Haskell. I’ve written a small program that

  • 0

I am a Java programmer who learns Haskell.
I’ve written a small program that searches files for words with a particular suffix.

I’d like to read your criticism.
What would you suggest to make this code more compact and readable?

module Main where

import Control.Monad
import Data.String.Utils
import Data.List
import Data.Char
import System.Directory
import System.FilePath
import System.IO
import System.IO.HVFS.Utils
import Text.Regex

alphaWords :: String -> [String]
alphaWords = words . map (\c -> if isAlpha c then c else ' ') -- by ephemient
-- was:
-- words2 s =  case dropWhile isSpace2 s of
--     "" -> []
--     ss -> w : words2 sss
--         where (w, sss) = break isSpace2 ss
--     where isSpace2 = not . isAlpha

findFiles :: FilePath -> IO [FilePath]
findFiles path = do
    cur_path <- getCurrentDirectory
    files <- recurseDir SystemFS $ normalise $ combine cur_path path
    filterM doesFileExist files

wordsWithSuffix :: String -> String -> [String]
wordsWithSuffix suffix text =
    let tokens = (nub . alphaWords) text
        endswithIgnoringCase = endswith suffix . map toLower
    in filter endswithIgnoringCase tokens

searchWords :: String -> String -> [String] -> IO [String]
searchWords suffix path exts = do
    let isSearchable = (`elem` exts) . takeExtension -- by yairchu
    --was let isSearchable s = takeExtension s `elem` exts

    --files <- filterM (fmap isSearchable) $ findFiles path -- by ephemient (compile error)
    files <- liftM (filter isSearchable) $ findFiles path

    wordsPerFile <- forM files $ fmap (wordsWithSuffix suffix) . readFile -- by ephemient
    -- was: wordsPerFile <- forM files (\x -> liftM (wordsWithSuffix suffix) (readFile x))

    return . sort . nub $ concat wordsPerFile -- by ephemient
    -- was: return $ (sort . nub . concat) wordsPerFile

main = do
    words <- searchWords "tick" "/path/to/src" [".as", ".java", ".mxml"]
    print $ length words
    putStrLn $ unlines words

UPDATE: I’ve fixed 2 verbose spots found using “hlint”, thanks @yairchu
UPDATE 2: More fixes. Thanks @ephemient
UPDATE 3: One little fix. Thanks @yairchu, can’t use all of your code – too hard for a Java dev’s mind

  • 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-11T23:27:26+00:00Added an answer on May 11, 2026 at 11:27 pm

    Don’t import System.FilePath.Posix if you don’t need to. System.FilePath exports System.FilePath.Posix or System.FilePath.Windows according to the platform you’re compiling on.

    Your words2 implementation is fine, but lacks any explanation as to why it does what it does. This is more self-explanatory, and the efficiency difference is not significant.

    alphaWords = words . map (\c -> if isAlpha c then c else ' ')
    

    Little improvements in searchWords:

    -    wordsPerFile <- forM files (\x ->
    -        liftM (wordsWithSuffix suffix) (readFile x))
    +    wordsPerFile <- forM files $ fmap (wordsWithSuffix suffix) . readFile
    -    return $ (sort . nub . concat) wordsPerFile
    +    return . sort . nub $ concat wordsPerFile
    

    Type annotations inside let constructs are not common unless the typechecker really needs assistance… though if I were paying attention to them I wouldn’t have made my earlier mistake of trying to move isSearchable 🙂

    Also, in main, I would change this:

    -    putStrLn $ unlines words
    +    mapM_ putStrLn words
    

    I’m not familiar with the modules exposed by MissingH; is System.IO.HVFS.Utils.recurseDir lazy? If not, adding System.IO.Unsafe.unsafeInterleaveIO may help with memory consumption when traversing a large directory tree.

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

Sidebar

Ask A Question

Stats

  • Questions 305k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, I wouldn't call that a class, but that's just… May 13, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer Instead of thinking of this as a special class implementing… May 13, 2026 at 9:10 pm
  • Editorial Team
    Editorial Team added an answer Compilers Compilers were the first sort of translator program to… May 13, 2026 at 9:10 pm

Related Questions

I am a Java programmer who learns Haskell. I've written a small program that
I would like to learn some VB because I am interested in a few
I have inherited a broad, ill-designed web portfolio at my job. Most pages are
As the question states, i am a C#/Java programmer who is interested in (re)learning
I'm a fairly competent Java programmer who's very new to C. I am trying

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.