caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "OCaml List (caml-list@inria.fr)" <caml-list@inria.fr>
Subject: RE: [Caml-list] still puzzled on generic types
Date: Thu, 29 Sep 2011 19:04:31 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9C248E3BD@Remus.metastack.local> (raw)
In-Reply-To: <alpine.LFD.2.00.1109291719440.4431@surtur.dico.unimi.it>

Walter Cazzola wrote:
> On Thu, 29 Sep 2011, David Allsopp wrote:
> 
> > I think it's probably something to do with optional arguments not
> > behaving as you expect.
> 
> uhm, would be interesting to see what is wrong on my use of the optional
> arguments.

Separately answered...

> > But this is a bad use for optional arguments - you should instead use
> > a nested function to pass the accumulator values. This works fine:
> 
> yes my first attempt was implemented with nested functions but ,,,
> 
> > let enumerate l =
> >  let rec enumerate acc n = function
> >    h::ls -> enumerate ((n, h)::acc) (n + 1) ls
> >  | [] -> List.rev acc
> > in
> >    enumerate [] 0 l
> 
> ... my nested function had a different name than the nextee function and I
> was passing l to it too. Could you explain me why your code works? in
> particular where does it take the list to enumerate? Silly question I know
> but it seems magic to me written in such a way

OCaml allows a name to hide a previous use of a name in the scope chain. The closest name moving up the "let .. in .." chains will always be the one used. Very occasionally that can cause some weird bugs but as the types will often differ, it's a clearer way of writing (rather than inventing arbitrary new names or using lots of prime symbols). I defined the innermost [enumerate] using the [function] keyword. You could equivalently write:

let rec enumerate acc n l =
  match l with
    h::ls -> ...

but the function keyword gives you that for free (it's the keyword for introducing an anonymous function with match and simply gives you a match over a single anonymous parameter).

> > Concatenating lists is also expensive in terms of the left list so
> > your function is about as slow as possible! Much better when aiming
> > for tail recursion, accumulate a reversed list and then reverse it in
> > the basis case.
> 
> Ok, thanks for the note, but efficiency issues are still far in my
> learning curve ;-)

Learning not to concatenate lists is part of learning tail recursion - it should be higher up your list ;o)

HTH,


David



  reply	other threads:[~2011-09-29 19:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-29 15:07 Walter Cazzola
2011-09-29 15:16 ` David Allsopp
2011-09-29 15:29   ` Walter Cazzola
2011-09-29 19:04     ` David Allsopp [this message]
2011-09-29 15:20 ` Pierre Chopin
2011-09-29 15:32   ` Walter Cazzola

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=E51C5B015DBD1348A1D85763337FB6D9C248E3BD@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    /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).