From: Brian Hurt <bhurt@spnz.org>
To: Vasili Galchin <vasiliocaml@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Patterns in ML vs OCaml ...
Date: Sun, 24 Oct 2004 13:41:11 -0500 (CDT) [thread overview]
Message-ID: <Pine.LNX.4.44.0410241328440.15838-100000@localhost.localdomain> (raw)
In-Reply-To: <20041024173552.96205.qmail@web53002.mail.yahoo.com>
First off, there's an Ocaml's beginner's list this would be more
appropriate for:
http://groups.yahoo.com/group/ocaml_beginners/
On Sun, 24 Oct 2004, Vasili Galchin wrote:
>
> Hello,
>
> I'm sorry this is a very elementary question (but I am pulling my hair out). With a function written in ML:
>
> fun filter_list P [] = [] |
> filter_list P (h :: t) = let val ft = filter_list P t
> in if P h then (h :: ft)
> else ft
> end;
A literal translation of this function into Ocaml would be:
let rec filter_list p = function
| [] -> []
| h :: t ->
let ft = filter_list p t in
if p h then
h :: ft
else
ft
;;
Of course, the problem with this function is that it's not tail recursive.
For sufficiently long lists (say, more than few tens of thousands of
elements) you'll blow the stack. A better implementation would be:
let filter_list p =
let rec loop accu = function
| [] -> List.rev accu
| h :: t ->
if p h then
loop (h :: accu) t
else
loop accu t
in
loop []
;;
Also note that this function already exists in the standard library with
the name List.filter. If this is the actual function you need, use the
standard library version instead.
>
> fun member (e, nil) = false |
> member (e , (h :: t)) = if (e=h) then true
> else member (e, t) ;
>
The literal translation of this function would be:
let rec member = function
| (e, []) -> false
| (e, (h :: t)) ->
if e = h then (* note: structural comparison *)
true
else
member (e, h)
The problem with this function is that every call requires the allocation
of a new tuple. A better implementation is probably:
let rec member e = function
| [] -> false
| h :: t ->
if e = h then
true
else
member e t
;;
Once again, this function exists in the standard library, with the name
List.mem (A variant, which uses referential equality instead of structural
equality, also exists with the name List.memq).
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
prev parent reply other threads:[~2004-10-24 18:32 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-24 17:35 Vasili Galchin
2004-10-24 18:41 ` Brian Hurt [this message]
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.LNX.4.44.0410241328440.15838-100000@localhost.localdomain \
--to=bhurt@spnz.org \
--cc=caml-list@inria.fr \
--cc=vasiliocaml@yahoo.com \
/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