caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Bruno Pagano <bpagano@free.fr>
To: Diego Olivier Fernandez Pons <FernandezPons@iFrance.com>
Cc: Vincent.Barichard@info.univ-angers.fr, Caml <caml-list@inria.fr>
Subject: Re: [Caml-list] Function call with a list of parameters
Date: Thu, 13 Dec 2001 01:20:03 +0100	[thread overview]
Message-ID: <20011213012003.A29826@osiris.net-nono.com> (raw)
In-Reply-To: <00a201c18360$f8135480$074cf2c3@Utilisateur>; from FernandezPons@iFrance.com on Wed, Dec 12, 2001 at 11:31:53PM +0100

On Wed, Dec 12, 2001 at 11:31:53PM +0100, Diego Olivier Fernandez Pons wrote:

One wants write the following function 

# let rec app f  = function 
    [] -> f 
  | x::l -> app (f x) l    ;;

but caml reply :

This expression has type 'a but is here used with type 'b -> 'a


Thanks to OOP in caml ;-) , we can use cyclics types (-rectypes option)
and now :

# let rec app f  = function 
    [] -> f 
  | x::l -> app (f x) l    ;;
val app : ('b -> 'a as 'a) -> 'b list -> 'a = <fun>


So what about the first argument of app. I need a function with 'a -> 'b
as type, it must be a function with no termination. Ok, the result may
be raised by an exception.
For instance :

exception Int of int

# let sum2 x y = raise (Int (x+y))
  let sum3 x y z = raise (Int (x+y+z)) ;;
val sum2 : int -> int -> 'a = <fun>
val sum3 : int -> int -> int -> 'a = <fun>

To end, the apply function will catch the result :

# let  apply  f l = try app f l ; 0 with Int x -> x ;;
Warning: this function application is partial,
maybe some arguments are missing.
val apply : ('b -> 'a as 'a) -> 'b list -> int = <fun>

We are more intelligent than the type checker and we ommit the warning ...

and now let's try :

# apply sum2 [1;2] ;;
- : int = 3

# apply sum3 [1;2;3] ;;
- : int = 6


Bruno Pagano
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-12-13 11:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-11 16:31 Vincent Barichard <Vincent Barichard
2001-12-11 22:59 ` Chris Hecker
2001-12-11 23:26   ` Bruce Hoult
2001-12-12  9:35   ` Markus Mottl
2001-12-12 10:20   ` Vincent Barichard <Vincent Barichard
2001-12-12 22:31     ` Diego Olivier Fernandez Pons
2001-12-13  0:20       ` Bruno Pagano [this message]
2001-12-13  0:17         ` Diego Olivier Fernandez Pons
2001-12-14 13:26           ` Alain Frisch
2001-12-17  7:40             ` Francois Pottier
2001-12-12 13:52   ` Francois Pottier
2001-12-12 18:54     ` Chris Hecker
2001-12-12 19:04       ` Patrick M Doane
2001-12-12 23:49       ` Bruce Hoult
2001-12-13  7:41       ` Francois Pottier
2001-12-12  9:31 ` Jim Farrand

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=20011213012003.A29826@osiris.net-nono.com \
    --to=bpagano@free.fr \
    --cc=FernandezPons@iFrance.com \
    --cc=Vincent.Barichard@info.univ-angers.fr \
    --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).