Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

[Caml-list] Mapping onto lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-07-18 (16:44) From: Jun Furuse Subject: RE: [Caml-list] Mapping onto lists
```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.

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

```