From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA03619; Wed, 24 Apr 2002 00:24:54 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA03640 for ; Wed, 24 Apr 2002 00:24:53 +0200 (MET DST) Received: from schroeder.cs.wisc.edu (schroeder.cs.wisc.edu [128.105.6.11]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g3NMOqv13925 for ; Wed, 24 Apr 2002 00:24:52 +0200 (MET DST) Received: from wireless02.cs.wisc.edu (wireless02.cs.wisc.edu [128.105.48.102]) by schroeder.cs.wisc.edu (8.11.3/8.11.3) with ESMTP id g3NMOlv31653; Tue, 23 Apr 2002 17:24:47 -0500 Subject: Re: [Caml-list] Stack Overflow... (recursion in try-statement) From: Will Benton To: Oliver Bandel Cc: "caml-list@inria.fr" In-Reply-To: References: Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.3.99 Date: 23 Apr 2002 17:23:26 -0500 Message-Id: <1019600606.1960.15.camel@schnauzer> Mime-Version: 1.0 X-Virus-Scanned: by AMaViS-perl11-milter (http://amavis.org/) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tue, 2002-04-23 at 16:48, Oliver Bandel wrote: > Hello, > > why does an stack overflow-error occur here? > > let rec traversedir dir = > try ( [Unix.readdir dir] @ traversedir dir ) with > End_of_file -> [];; > > I have not tried it with very deep directories, so > I did not expect such an error... > > What is the problem here? It looks to me like this will never terminate, since "traversedir dir" calls "traversedir dir". I don't have any O'Caml code to do what you want, but I do have some pseudocode (OK, it's python) to perform a depth-first traversal of a directory hierarchy, performing some action on every file that satisfies some predicate: def dirwalk(tree, predicate, action): for file in os.listdir(tree): fullpath = tree + "/" + file if os.path.isdir(fullpath) and not os.path.islink(fullpath): dirwalk(fullpath, predicate, action) else: if predicate(fullpath): action(fullpath) This could be transliterated into bad O'Caml trivially, and into idiomatic O'Caml without too much work. I'd do it for you, but I'm on my way out of the office. :-) Notice that I ignore symbolic links. This is necessary to avoid cycles, since a UNIX filesystem is a directed graph and not a tree. (Alternatively, you could keep track of what canonical paths you've visited and only descend down a tree that you haven't already marked.) best, wb ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners