caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Chris King" <colanderman@gmail.com>
To: ian <fist_187@softhome.net>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Style and organization of code
Date: Wed, 14 Mar 2007 22:44:29 -0500	[thread overview]
Message-ID: <875c7e070703142044v2bc61765i3ea00d463a8f77ed@mail.gmail.com> (raw)
In-Reply-To: <45F87661.4020504@softhome.net>

On 3/14/07, ian <fist_187@softhome.net> wrote:
> Say I have a function called "solveHardProblem".   solveHardProblem relies on
> several helper functions, which are not going to be useful to any other
> functions in the program.  So, my first instinct would be to define all the
> helpers using let blocks within the definition of solveHardProblem.

I usually do exactly that.  I think it's fine to have a long let like
this, so long as it's made up of small, manageable pieces.  The
biggest problem is that your arguments are now miles away from the
definition of your function, but this can be solved with something
like the following:

let solve_hard_problem =
    let solve_simpler_problem x = ...
    and solve_something_else x = ... in
    fun x y z ->
        [code goes here]

This is also a good trick for moving a global variable (such as a
unique ID counter or a hash table) into the scope of the function that
uses it.

FWIW I've seen some examples of this style in the standard library.
genlex.ml has a 150+ line function of the form:

let make_lexer keywords =
  let kwd_table = Hashtbl.create 17 in
  List.iter (fun s -> Hashtbl.add kwd_table s (Kwd s)) keywords;
  let ident_or_keyword id = ...
  and keyword_or_error c = ...
  [snip]
  in
  fun input -> Stream.from (fun count -> next_token input)

Since the "keywords" argument is needed by the helper functions, it
appears at the top in the let, but "input" isn't and so it was moved
to the bottom in the fun.  None of the helper functions are much
longer than one screenful so it makes for easy reading.


  parent reply	other threads:[~2007-03-15  3:44 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-14 22:25 ian
2007-03-15  3:21 ` [Caml-list] " Tom
2007-03-15  1:03   ` ian
2007-03-15  8:02     ` Gabriel Kerneis
2007-03-15  8:09       ` Maxence Guesdon
2007-03-15  8:20         ` Gabriel Kerneis
2007-03-15 10:11           ` ian
2007-03-15 20:04             ` Martin Jambon
2007-03-15  3:44 ` Chris King [this message]
2007-03-15  3:50   ` Chris King
2007-03-15  4:01 ` skaller
2007-03-15  9:03   ` micha
2007-03-15 13:38 ` dynamically finding libraries Vu Ngoc San
2007-03-15 17:10   ` [Caml-list] " Eric Cooper
2007-03-15 17:29     ` slightly OT: anyone reading this list with Mutt? Eric Cooper
2007-03-15 18:44       ` [Caml-list] " Stefano Zacchiroli
2007-03-15 19:00       ` caml-list
2007-03-15 19:44         ` ls-ocaml-developer-2006
2007-03-15 22:39 ` [Caml-list] Style and organization of code Richard Jones
2007-03-29  0:49 ` Jon Harrop
     [not found] <20070315224000.674E3BC82@yquem.inria.fr>
2007-03-15 23:08 ` David Allsopp
2007-03-15 23:48   ` Richard Jones
2007-03-16  3:51     ` Quôc Peyrot

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=875c7e070703142044v2bc61765i3ea00d463a8f77ed@mail.gmail.com \
    --to=colanderman@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=fist_187@softhome.net \
    /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).