caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: Tato Thetza <thetza@sent.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] recursion/iterator question
Date: Mon, 17 Apr 2006 15:09:36 +0200	[thread overview]
Message-ID: <44439390.7040102@inria.fr> (raw)
In-Reply-To: <1145221898.16349.259200911@webmail.messagingengine.com>

> Given a list, I would like to iterate over all triplets in the list. For
> example, in mathematcs, its not uncommon to have expressions such as
> "for all i,j,k in set X, do f(i,j,k)"
>
> The only way I can think of is to create a list with all triplets of the
> list, so:
>   triplets([1,2,3,4]) = [(1,2,3),(1,2,4),(1,3,4),(2,3,4)]
> and take this list and map a function f to it.

Folds are more general than maps and tend to compose better.
Consider:

let list_fold_right_3 f l1 l2 l3 =
  List.fold_right
    (fun x1 -> List.fold_right (fun x2 -> List.fold_right (f x1 x2) l3) l2)
    l1

Examples:

# list_fold_right_3 (fun x y z a -> (x,y,z)::a) [1;2] [3;4] [5;6] [];;
- : (int * int * int) list =
[(1, 3, 5); (1, 3, 6); (1, 4, 5); (1, 4, 6); (2, 3, 5); (2, 3, 6); (2,
4, 5);
 (2, 4, 6)]
# list_fold_right_3 (fun x y z a -> (x+y+z)::a) [1;2] [3;4] [5;6] [];;
- : int list = [9; 10; 10; 11; 10; 11; 11; 12]
# list_fold_right_3 (fun x y z a -> x*y*z + a) [1;2] [3;4] [5;6] 0;;
- : int = 231

But, yes, if you find yourself doing this often, it's a good idea to
use the syntax extension for list comprehensions.

- Xavier Leroy


      parent reply	other threads:[~2006-04-17 13:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-16 21:11 Tato Thetza
2006-04-16 22:00 ` [Caml-list] " David Powers
2006-04-16 22:27 ` Martin Jambon
2006-04-17  0:06 ` Jon Harrop
2006-04-17  9:36   ` Christian Stork
2006-04-17 17:07     ` Jon Harrop
2006-04-18  3:25       ` Jonathan Roewen
2006-04-18  8:58       ` Christian Stork
2006-04-18 16:15         ` Christian Stork
2006-04-20 11:53     ` Damien Doligez
2006-04-17  6:50 ` Li-Thiao-Té Sébastien
2006-04-17 11:26   ` Nils Gesbert
2006-04-17 13:09 ` Xavier Leroy [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=44439390.7040102@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=thetza@sent.com \
    /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).