From: Jun Furuse <Jun.Furuse@inria.fr>
To: franka@cs.uu.nl
Cc: jcriddle4@home.com, caml-list@inria.fr
Subject: RE: [Caml-list] Mapping onto lists
Date: Wed, 18 Jul 2001 18:44:50 +0200 [thread overview]
Message-ID: <20010718184450Z.Jun.Furuse@inria.fr> (raw)
In-Reply-To: <FLELJKKJEIKNBDJGMIHKOEFBCAAA.franka@cs.uu.nl>
Hello,
From: "Frank Atanassow" <franka@cs.uu.nl>
> > Is there any way to combine these two functions(map and my_map) so
> > that the following will work?
>
> In short, no, but perhaps it will help to point out that:
>
> my_map x = map (fun f -> f x)
>
> Maybe it is possible in G'Caml, though.
Yes. It is a simple overloading example. :-)
let rec fmap v = function
| [] -> []
| f::fs -> f v :: fmap v fs
;;
generic rec combined_map = case
| ($a -> $b) -> $a list -> $b list => List.map
| $a -> ($a -> $b) list -> $b list => fmap
;;
# combined_map ((+) 2) [10; 100; 100];;
- : int list = [12; 102; 102]
# combined_map 2 [(+) 10; (+) 100; (+) 1000];;
- : int list = [12; 102; 1002]
The code of the original post:
>> let plus x y = x + y;;
>>
>> let rec map f = function
>> [] -> []
>> | head::tail -> let r = f head in r :: map f tail;;
>>
>> map (plus 2) [10; 100; 100];;
>> - : int list = [12; 102; 1002]
>>
>> let rec my_map f = function
>> [] -> []
>> | head::tail -> let r = head f in r :: my_map f tail;;
>>
>> my_map 10 [plus 2; plus 4; plus 7];;
>> - : int list = [12; 14; 17]
>>
>> combined_map 2 [plus 10; plus 100; plus 1000];;
>> combined_map (plus 2) [10; 100; 100];;
--
JPF
-------------------
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
prev parent reply other threads:[~2001-07-18 16:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-07-18 14:00 Jay Riddle
2001-07-18 16:11 ` Frank Atanassow
2001-07-18 16:44 ` Jun Furuse [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=20010718184450Z.Jun.Furuse@inria.fr \
--to=jun.furuse@inria.fr \
--cc=caml-list@inria.fr \
--cc=franka@cs.uu.nl \
--cc=jcriddle4@home.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;
as well as URLs for NNTP newsgroup(s).