Drkcore

08 12 2009 Haskell Tweet

HaskellでEmacsのバックアップファイルを探す

9章のforMの練習

forM :: (Monad m) => [a] -> (a -> m b) -> m [b]

リストと、関数をとってリストのモナドを返す

module RecursiveContents (getRecursiceContents) where

import Control.Monad (forM)
import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.FilePath ((</>))
import Text.Regex.Posix ((=~))

getRecursiceContents :: FilePath -> IO [FilePath]
getRecursiceContents topdir = do
  names <- getDirectoryContents topdir
  let properNames = filter (`notElem` [".", ".."]) names
  paths <- forM properNames $ \name -> do
    let path = topdir </> name
    isDirectory <- doesDirectoryExist path
    if isDirectory
       then getRecursiceContents path
       else 
           if path =~ ".+~$"
           then return [path]
           else return []
  return (concat paths)

実行

$ ghci RecursiveContents.hs 
*RecursiveContents> getRecursiceContents "/Users/kzfm/perl"
["/Users/kzfm/perl/hori.pl~","/Users/kzfm/perl/Moosp/lib/Moosp/Function \
/Defun.pm~","/Users/kzfm/perl/Moosp/t/17-function-defun.t~"]

ProductName Real World Haskell―実戦で学ぶ関数型言語プログラミング
Bryan O'Sullivan,John Goerzen,Don Stewart
オライリージャパン / ¥ 3,990 ()
在庫あり。

doesDirectoryExist関数はアクションで返り値はIO Boolのため、<-でBoolにする必要がある。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021