How to make this code more compact and readable?

I am a Java programmer who is studying Haskell. I wrote a small program that searches for files for words with a specific suffix.

I would 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 have set 2 detailed points found using "hlint", thanks @yairchu
UPDATE 2: Additional fixes. Thanks @ephemient
UPDATE 3: One small fix. Thanks @yairchu, can't use all your code - too complicated for Java dev mind

+3
source share
3 answers

import System.FilePath.Posix, . System.FilePath System.FilePath.Posix System.FilePath.Windows , .

words2 , , , . , .

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

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

let , typechecker ... , , , isSearchable:)

, main :

-    putStrLn $ unlines words
+    mapM_ putStrLn words

, MissingH; System.IO.HVFS.Utils.recurseDir ? , System.IO.Unsafe.unsafeInterleaveIO .

+4

, hlint.

, :

homework.hs:46:1: Error: Use print
Found:
  putStrLn $ show $ length words
Why not:
  print (length words)

, print = putStrLn . show

..

+2

I don't like variable names.

Therefore, here is shorter searchWords:

searchWords :: String -> String -> [String] -> IO [String]
searchWords suffix path exts =
  fmap (sort . nub . concatMap (wordsWithSuffix suffix)) .
  mapM readFile . filter ((`elem` exts) . takeExtension)
  =<< findFiles path
+2
source

Source: https://habr.com/ru/post/1712013/


All Articles