[Haskell-cafe] Infinite loop?

rgo rgo at mail.interzet.ru
Fri Feb 17 15:12:36 EST 2006


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;


More information about the Haskell-Cafe mailing list