caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: John Prevost <visigoth@cs.cmu.edu>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Unix-Filekind => codexample with questions
Date: Tue, 23 Apr 2002 16:45:46 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.LNX.3.95.1020423162229.462A-100000@first.in-berlin.de> (raw)
In-Reply-To: <86u1q421lf.fsf@laurelin.dementia.org>



On 21 Apr 2002, John Prevost wrote:

> >>>>> "ob" == Oliver Bandel <oliver@first.in-berlin.de> 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


      parent reply	other threads:[~2002-04-23 15:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-21 20:50 Oliver Bandel
2002-04-21 21:04 ` Remi VANICAT
2002-04-21 21:17 ` John Prevost
2002-04-21 21:56   ` Markus Mottl
2002-04-23 14:45   ` Oliver Bandel [this message]

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=Pine.LNX.3.95.1020423162229.462A-100000@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=caml-list@inria.fr \
    --cc=visigoth@cs.cmu.edu \
    /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;
as well as URLs for NNTP newsgroup(s).