* [Caml-list] Mapping onto lists
@ 2001-07-18 14:00 Jay Riddle
2001-07-18 16:11 ` Frank Atanassow
0 siblings, 1 reply; 3+ messages in thread
From: Jay Riddle @ 2001-07-18 14:00 UTC (permalink / raw)
To: caml-list
Is there any way to combine these two functions(map and my_map) so
that the following will work?
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 mapping function *)
combined_map 2 [plus 10; plus 100; plus 1000];;
combined_map (plus 2) [10; 100; 100];;
--JayR x8-1335
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] Mapping onto lists
2001-07-18 14:00 [Caml-list] Mapping onto lists Jay Riddle
@ 2001-07-18 16:11 ` Frank Atanassow
2001-07-18 16:44 ` Jun Furuse
0 siblings, 1 reply; 3+ messages in thread
From: Frank Atanassow @ 2001-07-18 16:11 UTC (permalink / raw)
To: Jay Riddle, caml-list
> 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.
---
Frank Atanassow, Information & Computing Sciences, Utrecht University
Padualaan 14, PO Box 80.089, 3508TB Utrecht, The Netherlands
Tel +31 (0)30 253-3261 Fax +31 (0)30 251-3791
---
> 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 mapping function *)
> combined_map 2 [plus 10; plus 100; plus 1000];;
> combined_map (plus 2) [10; 100; 100];;
-------------------
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* RE: [Caml-list] Mapping onto lists
2001-07-18 16:11 ` Frank Atanassow
@ 2001-07-18 16:44 ` Jun Furuse
0 siblings, 0 replies; 3+ messages in thread
From: Jun Furuse @ 2001-07-18 16:44 UTC (permalink / raw)
To: franka; +Cc: jcriddle4, caml-list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2001-07-18 16:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-18 14:00 [Caml-list] Mapping onto lists Jay Riddle
2001-07-18 16:11 ` Frank Atanassow
2001-07-18 16:44 ` Jun Furuse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox