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 RAA28760; Tue, 23 Apr 2002 17:10:31 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA28774 for ; Tue, 23 Apr 2002 17:10:30 +0200 (MET DST) Received: from hirsch.in-berlin.de (hirsch.in-berlin.de [192.109.42.6]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g3NFATP20124 for ; Tue, 23 Apr 2002 17:10:29 +0200 (MET DST) Received: from hirsch.in-berlin.de (localhost [127.0.0.1]) by hirsch.in-berlin.de (8.12.1/8.12.1/Debian -2) with ESMTP id g3NFA87R020065 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NOT); Tue, 23 Apr 2002 17:10:08 +0200 Received: (from uucp@localhost) by hirsch.in-berlin.de (8.12.1/8.12.1/Debian -2) with UUCP id g3NFA8qL020063; Tue, 23 Apr 2002 17:10:08 +0200 X-Envelope-From: oliver@first.in-berlin.de X-Envelope-To: caml-list@inria.fr Received: from localhost (oliver@localhost) by first.in-berlin.de (8.7.6/8.7.3) with SMTP id QAA00636; Tue, 23 Apr 2002 16:45:47 +0200 Date: Tue, 23 Apr 2002 16:45:46 +0200 (MET DST) From: Oliver Bandel To: John Prevost cc: caml-list@inria.fr Subject: Re: [Caml-list] Unix-Filekind => codexample with questions In-Reply-To: <86u1q421lf.fsf@laurelin.dementia.org> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On 21 Apr 2002, John Prevost wrote: > >>>>> "ob" == Oliver Bandel writes: > > ob> Hello, I need a function, which tells me if a file is a > ob> regular file or not. > > ob> Please look at my code-example: > > let is_regularfile name = > let filekind name = let tmp = Unix.stat name in tmp.Unix.st_kind > in > let fk = try (filekind name ) with > | Unix.Unix_error (Unix.ENOENT,_,_) -> Unix.S_DIR > | _ -> Unix.S_DIR > > in > match fk with > | Unix.S_REG -> true > | _ -> false;; > > > ob> This example works: It tells me, if a file is of kind/type > ob> regular file with true. If it is not a regular file or an > ob> error occured, it tells me this by passing me the result > ob> false. > > How about: > > let is_regularfile name = > try (match Unix.stat name with > | {Unix.st_kind = Unix.S_REG} -> true > | _ -> false) > with > _ -> false > > I'm not sure why you're using so many intermediate values here--it > just seems to make the code harder to figure out. Better to just wrap > a try around the whole check, and use a small simple match on the stat > result. I really started with more than one functions and then only put them together into one function, to have a cleaner function (not giving the Unix.S_DIR (as an indicator of not-Unix.S_REG)i as returnvalue to the outside). I thought about pattern-matching the records, and to use the ".member"-notation of records directly to the application of the stat-function (as in Markus' example), but I didn't know if OCaml's syntax provides the latter one. So I used my first working "one-function-only"-version and asked you all for comments. For example the part with > let fk = try (filekind name ) with > | Unix..Unix_error (Unix.ENOENT,_,_) -> Unix.S_DIR > | _ -> Unix.S_DIR may be too verbose and does not make any sense, because both cases return the same value and the "_"-match matches on all cases. But I'm relativley new to Ocaml and therefore I can see the details in matching the error-value better, when written explicitly (the first case) and it's easier to expand the matching, when using copy&paste&change of that line (changing Unix.ENOENT to the needed value after duplicating the line). So, some maybe unnecessary looking constructs in the code were helpers for understanding, what I'm doing. With more experience in OCaml-programming I maybe do not need such verbose code (and I really like brevity - that's one of the main-reasons to choose OCaml as my favourite programming language for future projects). But nevertheless, it's always worth to think about verbose coding of programs, if that clarifies the intention. At least in C-programming, writing more code than less may help in understanding, what's going on. [1] Maybe it's different in OCaml, because it's code does contain enough sense per command-word/line of code. In C there is too much bloat; and more verbose coding can (may) help in understanding. So the verbose coding of my example was a result of too-many-years-C-programming and too less experience in Ocaml-programming. All answers had helped a lot. Ciao, Oliver [1]: I like the literate programming idea; e.g. haskell provides two programming styles: code with inserted comments, or text with inserted code; so programming and documenting the code means, to edit *one* document, but to get the program and the documentation as a result. And in this way it's much easier and more motivating to write documentation of code-snippets (and the real advantage comes in big projects). All other (non-literate-programming) attempts in programming (programming here means: design+coding+documenting) I have seen, doesn't worked well. ------------------- 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