List Info

Thread: Re: "ocaml_beginners"::[] generic function on Set




Re: "ocaml_beginners"::[] generic function on Set
country flaguser name
France
2007-05-04 11:02:00

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.

MODULE SET(S : SET.S, T : SET.S) = STRUCT
LET EMPTY = S.EMPTY;;
LET FOLD = S.FOLD;;
LET ADD = S.ADD;;

LET MAP F SET =
LET G = FUN E SET -> S.ADD (F E) SET IN
S.FOLD G SET S.EMPTY;;
LET MAP_TO F SET =
LET G = FUN E SET -> T.ADD (F E) SET IN
S.FOLD G SET T.EMPTY;;
END

AM I COMPLETELY WRONG OR IS THERE A SIMPLE WAY TO DO THIS ?

THANKS,
MATHIAS

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic function on Set
country flaguser name
United States
2007-05-04 11:39:12

On Fri, 4 May 2007, Mathias Kende wrote:

> 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.
>
> module Set(S : Set.S, T : Set.S) = struct
> let empty = S.empty;;
> let fold = S.fold;;
> let add = S.add;;
>
>
> let map f set =
> let g = fun e set -> S.add (f e) set in
> S.fold g set S.empty;;
> let map_to f set =
> let g = fun e set -> T.add (f e) set in
> S.fold g set T.empty;;
> end
>
> Am I completely wrong or is there a simple way to do this ?

Functor arguments are curried. Replace the first line with

module Set(S : Set.S) (T : Set.S) = struct

Also, you probably don't want to name this module Set which will hide the
Set module in the stdlib.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy."

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic function on Set
country flaguser name
United States
2007-05-06 00:46:01
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/
 

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )