caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@gmail.com>
To: Richard Jones <rich@annexia.org>
Cc: Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Are map and iter guaranteed to be called in forwards order?
Date: Tue, 17 Aug 2004 11:19:39 -0400	[thread overview]
Message-ID: <d849ad2a04081708194c102577@mail.gmail.com> (raw)
In-Reply-To: <20040817145653.GA12345@annexia.org>

On Tue, 17 Aug 2004 15:56:53 +0100, Richard Jones <rich@annexia.org> wrote:
> This came up because I wanted a sensible way to number a list of
> items.  The obvious imperative approach is:
> 
>   # let items = ['a';'c';'e';'g';'i'];;
>   val items : char list = ['a'; 'c'; 'e'; 'g'; 'i']
>   # let i = ref 0;;
>   val i : int ref = {contents = 0}
>   # let items = List.map (fun item -> let n = !i in incr i; n, item) items;;
>   val items : (int * char) list =
>     [(0, 'a'); (1, 'c'); (2, 'e'); (3, 'g'); (4, 'i')]
> 
> The functional approach is comparatively long-winded: you have to
> effectively write your own loop explicitly, and the obvious way to
> write it isn't tail recursive, so you have to do it with accumulators.
> 
> It'd be nicer to have a library HOF to do this.

How about fold?

let number l =
  let _, l = List.fold_left (fun (n, l) i -> (n+1, (n, i)::l)) (0, []) l in
  List.rev l

You could even do fold with side effects if you really want to:

let number' l =
  let i = ref 0 in
  let l = List.fold_left (fun l x -> let n = !i in incr i; (n, x) :: l) [] l in
  List.rev l

Or define your own version of map, that you have explicitly written to
be based on fold_left, which allows you to be sure that it will always
work the same way:

let folding_map f l =
  let l = List.fold_left (fun l x -> (f x)::l) [] l in
  List.rev l

John.

-------------------
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:[~2004-08-17 15:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-17 12:00 Richard Jones
2004-08-17 12:45 ` Damien Doligez
2004-08-17 14:26 ` John Prevost
2004-08-17 14:56   ` Richard Jones
2004-08-17 15:13     ` Anil Madhavapeddy
2004-08-17 15:17     ` Jean-Marie Gaillourdert
2004-08-17 15:19     ` John Prevost [this message]
2004-08-18  0:57   ` Jon Harrop
2004-08-18  5:11     ` skaller
2004-08-18  7:10       ` skaller

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=d849ad2a04081708194c102577@mail.gmail.com \
    --to=j.prevost@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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).