caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: caml users <caml-list@inria.fr>
Subject: [Caml-list] List.fold_left vs. Hashtbl.fold
Date: Wed, 28 Nov 2012 18:25:41 +0100	[thread overview]
Message-ID: <CAPFanBHXymazXgafbSj0Skm_eOzHDJnXDBEdqf1-ebsJGavVmQ@mail.gmail.com> (raw)
In-Reply-To: <CAPFanBG04BiwJuPkV80__Ondmg1N8OEg4DokiXqDReg3ycNBdA@mail.gmail.com>

(Sorry for duplicate send.)

There are two exported way to fold over lists:
  fold_left : ('acc -> 'e -> 'acc) -> ('acc -> 'e list -> 'acc)
  fold_right : ('e -> 'acc -> 'acc) -> ('e list -> 'acc -> 'acc)

The _left and _right suffixes indicate whether the "accumulator"
argument (the partial result of what has been folded so far) is passed
as the left or the right parameter of the function argument: ('acc ->
'e -> 'acc) for left, ('e -> 'acc -> 'acc) for right. The return type
of fold then lifts this argument type from processing an element to
processing the whole list: the initial 'acc value for is passed "on
the left" for fold_left (before the list argument), "on the right" for
fold_right (after the list argument).

That's for the type. Regarding the implementation, fold_left applies
the argument function starting from "the left" (the beginning) of the
list first, while fold_right begins "on the right" (at the end): for
some infix operator (++) :
  fold_left (++) init [x; y; z]     is  ((init ++ x) ++ y) ++ z
  fold_right (++) [x; y; z] init   is (x ++ (y ++ (z ++ init)))
(The evaluation "begins" in the innermost nested parentheses)

Those two iterations techniques are available for linear data
structures (those that essentially look like lists). For more general
algebraic datatypes there is a canonical "folding" method, that
happens to coincide with fold_right on lists. The reason why
fold_right is "canonical" is that it turns the list
  x :: (y :: (z :: []))
into
  (x ++ (y ++ (z ++ init)))
replacing each constructor with a passed-in operator.

This means that fold_left may not exist for arbitrary data structure
(think of binary trees), while fold_right is rather universal. Other
folding functions in the standard library (in Set and Map in
particular, but in Hashtbl as well) have therefore taken inspiration
from the fold_right structure, rather than fold_left.

On Wed, Nov 28, 2012 at 5:40 AM, William Smith <bills@wwayneb.com> wrote:
>
> Hashtbl.fold expects the Hasthbl as the second parameter with the 3rd
> parameter being the initial value... just the opposite of List.fold_left.

  parent reply	other threads:[~2012-11-28 17:26 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-28  4:40 William Smith
2012-11-28 16:17 ` Lukasz Stafiniak
2012-11-28 16:25   ` Malcolm Matalka
2012-11-28 16:21 ` David House
2012-11-29  1:06   ` Francois Berenger
2012-11-28 16:42 ` Oliver Bandel
2012-11-28 17:11   ` Adrien
2012-11-28 17:41     ` Virgile Prevosto
2012-11-29  0:07       ` Jacques Garrigue
     [not found] ` <CAPFanBG04BiwJuPkV80__Ondmg1N8OEg4DokiXqDReg3ycNBdA@mail.gmail.com>
2012-11-28 17:25   ` Gabriel Scherer [this message]
2012-11-28 17:37     ` Lukasz Stafiniak
2012-11-30  3:06 William Smith
2012-11-30  9:53 ` Gabriel Scherer
2012-11-30 10:06   ` Stefano Zacchiroli
2012-11-30 16:00     ` Gabriel Scherer
2012-11-30 16:48       ` Daniel Bünzli
2012-11-30 11:34   ` Daniel Bünzli

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=CAPFanBHXymazXgafbSj0Skm_eOzHDJnXDBEdqf1-ebsJGavVmQ@mail.gmail.com \
    --to=gabriel.scherer@gmail.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).