Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Simon Peyton-Jones <simonpj@microsoft.com>
To: Pierre Weis <Pierre.Weis@inria.fr>, hwxi@ECECS.UC.EDU
Cc: caml-list@inria.fr
Subject: RE: reference initialization
Date: Sat, 20 May 2000 13:13:52 -0700	[thread overview]
Message-ID: <9E6D5163D780C142B2B58107F4E605A9021C994B@red-msg-23.redmond.corp.microsoft.com> (raw)

| val initialize :
|  int -> 'a -> ((int -> 'a) -> (int -> 'a -> unit) -> unit) -> 'a array
|  [initialize n x f] returns a fresh array of length [n],
|  with elements initialized by function [f].
|  All the elements of the new array must be assigned once and only
|  once by the function [f]. [f] received two functions as arguments,
|  one to access elements of the new array, and the other to set the
|  elements of the new array. [f] can access to element [i] of the new
|  array provided [f] has already properly initialized element [i].

This is all good stuff.  It might be worth mentioning Haskell's approach
in this context.  The main array former is

	array :: Ix ix => (ix,ix) -> [(ix,elt)] -> Array ix elt

'array' takes an upper and lower bound, both of type 'ix'.  ix is a type
variable that must be bound to a type in class Ix, which supports indexing
operations.  So that I can avoid discussion of type classes, let's
specialise
array much as 'initialize' is above, to vectors indexed by Ints:

	array :: (Int,Int) -> [(Int,elt)] -> Array Int elt

So array takes an uppper and lower bound, and a list of (index,value) pairs;
it returns an array with each element filled in.  The list should mention
each element just once, but that is not statically checked.

In conjunction with list comprehensions, this gives rise to quite nice code.
For example, to tabulate a function we might write

	array (1,n) [(i, f i) | i <- [1..n]]

Referring to existing elements is easy via recursion:

	fibs = arrray (1,n) ([(1,1),(2,1)] ++
			       [(i, fibs!(i-1) + fibs!(i-2)) | i <- [3..n]])

Notice how easily the boundary cases are specified.

Laziness means that the compiler does not need to figure out which
order to do the initialisation. In a strict language it would be a little
harder -- or perhaps one could say "it's done in the order the index,value
pairs are given".


You may wonder about the efficiency issue.  After all, it doesn't seem
great to build an intermediate list just before constructing an array.
But a bit of short-cut deforestation does the trick, and eliminates the
intermediate list.  I have to admit that a lot of things have to Work Right
in the compiler to really get the for-loop you intended, but that's what
compilers are for.


None of this is specific to Haskell or to a lazy language.  Caml could
well use it.  I mention it on this list becuase it's an aspect of the
Haskell design that I think has worked particularly well, and which 
might be of use to Camlers.

A lot of the benefit comes from the re-use (for arrays) of the 
list comprehension notation.  I forget whether Caml has such notation,
but again, it's nothing to do with laziness and it's a notation which
is really useful.

Simon





             reply	other threads:[~2000-05-22 16:12 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-20 20:13 Simon Peyton-Jones [this message]
2000-05-22 16:10 ` Pierre Weis
  -- strict thread matches above, loose matches on Subject: below --
2000-05-11 14:28 Stephanie Weirich
2000-05-12 20:38 ` Hongwei Xi
2000-05-15  8:49   ` Xavier Leroy
2000-05-15 17:47     ` Hongwei Xi
2000-05-15 21:33       ` Pierre Weis
2000-05-16  2:53         ` Hongwei Xi
2000-05-18 16:16           ` Pierre Weis
2000-05-19  6:54             ` Max Skaller
2000-05-22 15:28               ` Pierre Weis
2000-05-22 22:29                 ` Max Skaller
2000-05-15 22:20       ` Dave Mason
2000-05-15  9:36   ` Eijiro Sumii
2000-05-11 13:48 Dave Berry
2000-04-25 18:16 Caml wish list Pierre Weis
2000-05-10  4:50 ` reference initialization Hongwei Xi
2000-05-11 13:58   ` Pierre Weis
2000-05-11 18:59     ` Hongwei Xi
2000-05-12 17:07       ` Pierre Weis
2000-05-12 19:59         ` Hongwei Xi
2000-05-15  6:58           ` Max Skaller
2000-05-15 17:56             ` Hongwei Xi
2000-05-14 14:37         ` John Max Skaller
2000-05-13  7:07       ` Daniel de Rauglaudre
2000-05-13  7:09       ` Daniel de Rauglaudre
2000-05-11 16:02   ` John Prevost

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=9E6D5163D780C142B2B58107F4E605A9021C994B@red-msg-23.redmond.corp.microsoft.com \
    --to=simonpj@microsoft.com \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=hwxi@ECECS.UC.EDU \
    /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