
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;

On 2006-02-17 at 20:12GMT rgo wrote:
Hi all, my program probably goes into infinite loop... But i cannot understand where and why.
getDirectoryContents will include "." and "..", so if you follow those, you're bound to loop. -- Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk

Yep. change one line to:
entry <- if isdir && name /= "." && name /= ".."
and it does in fact work.
Jared.
On 2/17/06, Jon Fairbairn
On 2006-02-17 at 20:12GMT rgo wrote:
Hi all, my program probably goes into infinite loop... But i cannot understand where and why.
getDirectoryContents will include "." and "..", so if you follow those, you're bound to loop.
-- Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- http://www.updike.org/~jared/ reverse ")-:"

On Fri, 17 Feb 2006 17:26:08 +0000
Jon Fairbairn
On 2006-02-17 at 09:22PST Jared Updike wrote:
Yep. change one line to:
entry <- if isdir && name /= "." && name /= ".."
and it does in fact work.
Only if no-one has been tricky with symbolic links.
And how can you, in Haskell, detect symbolic links ? -- You have an unusual understanding of the problems of human relationships.

Barbier de Reuille Pierre
And how can you, in Haskell, detect symbolic links ?
I use System.Posix.Files. getSymbolicLinkStatus is like lstat(2) under Unix. It returns a value to which you can apply the isSymbolicLink predicate. Of course, you can't be sure the file won't change in between you checking and you acting on that result. -- Mark

On Fri, 17 Feb 2006 17:19:53 +0000
Jon Fairbairn
On 2006-02-17 at 20:12GMT rgo wrote:
Hi all, my program probably goes into infinite loop... But i cannot understand where and why.
getDirectoryContents will include "." and "..", so if you follow those, you're bound to loop.
-- Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk
Thanks. It's the third time in my life when do this error. :-[ here working code: import System.Directory data MyFile = MyDir { dir_name :: FilePath, dir_files :: [MyFile] } | MyFile { file_name :: FilePath } read_dir_entries :: FilePath -> [FilePath] -> IO [MyFile] read_dir_entries _ [] = return [] read_dir_entries dirname (name:names) = do isdir <- doesDirectoryExist name; entry <- if (isdir && (name /= ".") && (name /= "..")) then read_dir (dirname ++ "/" ++ name) else return (MyFile {file_name = name}); entries <- read_dir_entries dirname names; return (entry:entries) read_dir :: FilePath -> IO MyFile read_dir name = do content <- getDirectoryContents name; files <- read_dir_entries name 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;

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 ..)?
participants (7)
-
Barbier de Reuille Pierre
-
Dmitry V'yal
-
Jared Updike
-
Jon Fairbairn
-
mark@ixod.org
-
Ralf Hinze
-
rgo