[Haskell-cafe] Re: do/if/then/else confusion

Maurí­cio briqueabraque at yahoo.com
Fri Nov 2 02:23:59 EDT 2007


 > (...)
 > Can someone tell me what's wrong with this?
 > import qualified System.Posix.Directory as PD
 >
 > readdirAll :: PD.DirStream -> IO [String]
 > readdirAll d =
 >   do dir <- PD.readDirStream d
 >      if dir == ""
 >        then return []
 >        else rest <- readdirAll d
 >             return (dir:rest)
 > (...)

I don't know if this helps or disturbs, but I
wrote a few different versions of your code
as an exercise. If you want to try it, just
uncomment the versions you want to check. The
complete program below lists all files in
current directory, using 'readdirAll' to get
the full list.

Best,
Maurício


module Main (Main.main) where
import qualified System.Posix.Directory as PD
import Data.List
import Control.Monad

main :: IO ()

readdirAll :: PD.DirStream -> IO [String]
readdirAll ds = liftM reverse $ read []
   where
     read ("":t) = return t
     read list = (PD.readDirStream ds) >>= glue
       where glue f = read (f:list)
{-
readdirAll ds = read
   where
     read = (PD.readDirStream ds) >>= rest
     rest "" = return []
     rest h = liftM (h:) read
-}
{-
readdirAll ds = do
     f <- PD.readDirStream ds
     rest f
   where
     rest "" = return []
     rest h = return (h:) `ap` (readdirAll ds)
-}
main = (PD.openDirStream ".") >>=
      readdirAll >>= (putStrLn.show)



More information about the Haskell-Cafe mailing list