On Fri, 4 May 2007, Mathias Kende wrote:
> Le jeudi 03 mai 2007 Ã 17:09 -0500, Dave Benjamin a
écrit :
>> Rather than pass these functions directly, you can
use a functor:
>>
>> module Set_util(S : Set.S) = struct
>> let map f set =
>> let g = fun e set -> S.add (f e) set in
>> S.fold g set S.empty
>> end
>>
>
> Thank you for this help. I'm facing a new problem : I
want to "map" from
> one Set to another. So I tried to build this new Set
with two parameters
> (the Set from and to which we map). But I got a syntax
error on the
> declaration, and I saw nothing in the manual about
functors using more
> than one parameter.
You can have multiple (well, curried) modules as parameters
to a functor;
it's just a matter of getting the syntax right, as William
illustrated. In
this case, you don't necessarily need both modules as
parameters, though.
You could use a module to bind "add" and
"empty", and leave "fold" as a
parameter, like this:
module Set_util(S : Set.S) = struct
let map f set =
let g e set = S.add (f e) set in S.fold g set
S.empty
let map_with_fold fold f set =
let g e set = S.add (f e) set in (fold g set S.empty
: S.t)
end
module Int_set = Set.Make(struct
type t = int
let compare = compare
end)
module String_set = Set.Make(struct
type t = string
let compare = compare
end)
module Int_set_util = Set_util(Int_set)
module String_set_util = Set_util(String_set)
let () =
let int_set = List.fold_right Int_set.add [1; 2; 3]
Int_set.empty in
let string_set =
String_set_util.map_with_fold Int_set.fold
(fun x -> Printf.sprintf "#%d" x)
int_set in
Printf.printf "%sn"
(String.concat ", " (String_set.elements
string_set))
This will output "#1, #2, #3" by mapping an int
set containing {1, 2, 3}
to a string set containing {"#1", "#2",
"#3"}.
Note that "map_with_fold" is actually more
general; it doesn't require the
input to be a map at all, as long as the fold function takes
its
parameters in the right order. For example:
# String_set.elements
(String_set_util.map_with_fold List.fold_right
(fun x -> x) ["hello";
"world"]);;
- : String_set.elt list = ["hello";
"world"]
I'm not sure if any of this is practical. =)
Cheers,
Dave
[Non-text portions of this message have been removed]
Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc.
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http:/
/groups.yahoo.com/group/ocaml_beginners/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
ht
tp://groups.yahoo.com/group/ocaml_beginners/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:ocaml_beginners-digest@yahoogroups.com
mailto:ocaml_beginners-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
ocaml_beginners-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|