From: Will Benton <willb@cs.wisc.edu>
To: Oliver Bandel <oliver@first.in-berlin.de>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Stack Overflow... (recursion in try-statement)
Date: 23 Apr 2002 17:23:26 -0500 [thread overview]
Message-ID: <1019600606.1960.15.camel@schnauzer> (raw)
In-Reply-To: <Pine.LNX.3.95.1020423233831.10014A-100000@first.in-berlin.de>
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
next prev parent reply other threads:[~2002-04-23 22:24 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-23 21:48 Oliver Bandel
2002-04-23 22:23 ` Will Benton [this message]
2002-04-23 22:50 ` Oliver Bandel
2002-04-23 23:25 ` John Prevost
2002-05-21 18:34 ` [Caml-list] Is CVS version of ocaml faster? John Max Skaller
2002-05-22 3:03 ` Jacques Garrigue
2002-05-22 16:29 ` John Max Skaller
2002-06-30 17:25 ` [Caml-list] ocamllex -- ambiguous regex John Max Skaller
2002-07-01 11:45 ` Xavier Leroy
2002-06-30 17:27 ` [Caml-list] Manual broken -- set John Max Skaller
2002-04-24 1:01 ` [Caml-list] Stack Overflow... (recursion in try-statement) Warp
2002-04-24 13:22 ` Oliver Bandel
2002-04-24 4:10 ` John Prevost
2002-04-24 4:52 ` Alan Schmitt
2002-04-24 5:31 ` Charles Martin
2002-04-24 13:25 ` Oliver Bandel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1019600606.1960.15.camel@schnauzer \
--to=willb@cs.wisc.edu \
--cc=caml-list@inria.fr \
--cc=oliver@first.in-berlin.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox