caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Prevost <j.prevost@cs.cmu.edu>
To: caml-list@inria.fr
Subject: RE: Functional composition operator?
Date: Tue, 8 Dec 1998 15:32:17 -0500 (EST)	[thread overview]
Message-ID: <Pine.SUN.3.95L.981208151843.12707A-100000@deneb.fac.cs.cmu.edu> (raw)
In-Reply-To: <199812081808.SAA01875@byrd.sharp.co.uk>

On Tue, 8 Dec 1998, Andrew Kay wrote:

> In the end we settled on >> and << for forward and reverse
> composition respectively, satisfying the equations:
  {...}
> We're still interested to know if other people have different approaches.

This seems to be a very reasonable approach!  I'd been thinking recently
about this very question, and also about reverse composition, and the
chevrons handle this distinction quite nicely.

As for the person who said that composition isn't necessary:  there are
places where it is useful, in any case.  Take the following example of
an extensible printf system using continuation-passing in O'Caml
(translated from an SML version shown to me by Franklin Chen):

let id x = x

(* continuation, string, args *)
let f_int    k s x = k (s ^ string_of_int x)

let f_str    k s x = k (s ^ x)

let f_lit  x k s   = k (s ^ x)

let f_eol    k s   = k (s ^ "\n")

let f_list t k s = function
                   | [] -> k (s ^ "[]")
                   | xs -> let rec loop l k s =
                       match l with
                        | [x] ->     t (fun s -> k (s ^ "]")) s x
                        | (x::xs) -> t (fn s => loop xs k (s ^ ", ")) s x
                     in loop xs k (s ^ "[")

let printf c = c id ""

(* "3 is foo[2, 3, 4]\n" *)
let foo =
  printf (f_int $ f_lit " is " $ f_str $ (f_lis f_int) $ f_eol)
    3 "foo" [2, 3, 4];;

where $ is normal functional composition.

Notice that this definition of formatting has similar (although not
exactly the same, because it only support writing out a string (this
could be fixed, and possibly in a generic way)) nice properties to
O'Caml's Printf.printf stuff and formats, but doesn't require the
compiler to know about a special type of formats.  Note also that
composition serves a very good purpose in this case--you couldn't do
this easily without it, or with a prefix composition operator.

jmp.






  reply	other threads:[~1998-12-09  9:11 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-08 18:08 Andrew Kay
1998-12-08 20:32 ` John Prevost [this message]
1998-12-09 16:17 ` Anton Moscal
  -- strict thread matches above, loose matches on Subject: below --
1998-12-09 12:00 Don Syme
1998-12-08 20:09 Don Syme
1998-12-08 19:51 Don Syme
1998-12-08  5:23 John Whitley
1998-12-08 17:02 ` Pierre Weis
1998-12-08 21:52   ` John Harrison
1998-12-09 10:58     ` Pierre Weis
1998-12-09 17:17       ` John Harrison
1998-12-11 13:57         ` Pierre Weis

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=Pine.SUN.3.95L.981208151843.12707A-100000@deneb.fac.cs.cmu.edu \
    --to=j.prevost@cs.cmu.edu \
    --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).