From: "Dwight VandenBerghe" <dwight@pentasoft.com>
To: "Pascal Zimmer" <zimmer@easynet.fr>, <caml-list@inria.fr>
Subject: Re: Instruction return
Date: Mon, 2 Jun 1997 09:23:19 -0700 [thread overview]
Message-ID: <199706021620.JAA20114@blaze.accessone.com> (raw)
(I apologize for no French version)
> let rech x v =
> let n = vect_length v in
> for i=0 to n-1 do
> if v.(i) = x then return true
> done; false;;
This is one of the two main control problems with functional programming,
IMHO.
The solution I use is:
exception Found_it
let rech x v =
try
for i = 0 to pred (vect_length v) do
if v.(i) = x then raise Found_it
done;
false
with Found_it -> true
You don't have to assign vect_length v to n; it is only evaluated once
anyway.
If iter is defined on the type of v, then you can also use
let rech x v =
try
let f x1 = if x1 = x then raise Found_it in
iter f v; false
with Found_it -> true
The other control problem is when you want the equivalent of:
x = foo(a);
bar();
return x;
If you try
let x = foo(a) in
bar();
x
then bar gets done first, instead of second. I use this instead:
let f x = bar(); x in
in f (foo a)
I haven't found any other serious problems with Caml's logic flow. These
two just take some getting used to.
Dwight
next reply other threads:[~1997-06-02 21:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-06-02 16:23 Dwight VandenBerghe [this message]
1997-06-02 21:41 ` Pierre Weis
-- strict thread matches above, loose matches on Subject: below --
1997-05-31 17:30 Pascal Zimmer
1997-06-02 14:15 ` Pierre Weis
1997-06-03 8:37 ` Vincent Poirriez
1997-06-03 10:24 ` Pierre Weis
1997-06-03 14:17 ` Vincent Poirriez
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=199706021620.JAA20114@blaze.accessone.com \
--to=dwight@pentasoft.com \
--cc=caml-list@inria.fr \
--cc=zimmer@easynet.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