[Haskell-cafe] Infinite loop?
Ralf Hinze
ralf at informatik.uni-bonn.de
Fri Feb 17 12:26:00 EST 2006
Am Freitag 17 Februar 2006 21:12 schrieb rgo:
> Hi all,
> my program probably goes into infinite loop... But i cannot understand where and why.
>
> code:
>
> import System.Directory
>
> data MyFile = MyDir {
> dir_name :: String,
> dir_files :: [MyFile]
> }
> | MyFile {
> file_name :: String
> }
>
> read_dir_entries :: [FilePath] -> IO [MyFile]
> read_dir_entries [] = return []
> read_dir_entries (name:names) = do
> isdir <- doesDirectoryExist name;
> entry <- if isdir
> then read_dir name
> else return (MyFile {file_name = name});
> entries <- read_dir_entries names;
> return (entry:entries)
>
>
> read_dir :: FilePath -> IO MyFile
> read_dir name = do
> content <- getDirectoryContents name;
> files <- read_dir_entries content;
> return MyDir {
> dir_name = name,
> dir_files = files
> }
>
> instance Show MyFile where
> show (MyDir {dir_name = name, dir_files = files}) =
> "\n" ++ name ++ ":\n" ++ show files
> show (MyFile {file_name = name}) = "\t" ++ name ++ "\n"
>
> main = do
> cwd <- getCurrentDirectory;
> dir <- read_dir cwd;
> print dir;
Maybe because the file system is a graph not a tree (. and ..)?
More information about the Haskell-Cafe
mailing list