From: Vincent Aravantinos <vincent.aravantinos@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] How an exception could be an argument
Date: Fri, 17 Feb 2012 13:49:58 -0500 [thread overview]
Message-ID: <4F3EA156.8010709@gmail.com> (raw)
In-Reply-To: <CANnJ5GdKgpk_E1F3B9GDS-SC3EnnvLTTOgGRgEsvbZL0S_VGag@mail.gmail.com>
The arguments are evaluated before being passed to the function.
So in your first test, "Nothing" is raised before even entering "<|||>".
In the second case you're circumventing this by copying directly "raise
Nothing" in the body of the function: this copy-pasting is not
equivalent since you now force the evaluation of "raise Nothing" to
happen within the scope of "try".
* The following could be a workaround:
# let (<|||>) (f1,arg1) (f2,arg2) = try f1 arg1 with Nothing -> try f2
arg2 with Nothing -> raise Nothing;;
val ( <|||> ) : ('a -> 'b) * 'a -> ('c -> 'b) * 'c -> 'b = <fun>
# ((fun _ -> raise Nothing),()) <|||> ((fun _ -> "jj"),());;
- : string = "jj"
It looks a bit heavy on this example because we are working with constants.
But generally the constants come from some function application so it
can fit in a not-so-ugly way in real code.
* If you don't care about the infix, the following induces less parentheses:
# let try_or_try f1 arg1 f2 arg2 = try f1 arg1 with Nothing -> try f2
arg2 with Nothing -> raise Nothing;;
val try_or_try : ('a -> 'b) * 'a -> ('c -> 'b) * 'c -> 'b = <fun>
# try_or_try (fun _ -> raise Nothing) () (fun _ -> "jj") ();;
- : string = "jj"
* Note that you can simplify the second try since "try blah with Nothing
-> raise Nothing" is actually equivalent to "blah"
This yields:
# let (<|||>) (f1,arg1) (f2,arg2) = try f1 arg1 with Nothing -> f2 arg2;;
val ( <|||> ) : ('a -> 'b) * 'a -> ('c -> 'b) * 'c -> 'b = <fun>
On 02/17/2012 01:16 PM, Pierre-Alexandre Voye wrote:
> (raise Nothing) <|||> "jj";;
> Exception: Nothing.
>
> But if I try :
> try (raise Nothing) with
> | Nothing -> (try "jj"with
> | Nothing -> raise Nothing);;
> - : string = "jj"
--
Vincent Aravantinos
Postdoctoral Fellow, Concordia University, Hardware Verification Group
http://users.encs.concordia.ca/~vincent
next prev parent reply other threads:[~2012-02-17 18:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-17 18:16 Pierre-Alexandre Voye
2012-02-17 18:33 ` Edgar Friendly
2012-02-17 18:49 ` Vincent Aravantinos [this message]
2012-02-17 19:31 ` Tiphaine Turpin
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=4F3EA156.8010709@gmail.com \
--to=vincent.aravantinos@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