You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the ocaml docmentation chapter functor, when I read this paragraph:
Note: Most set operations need to compare elements to check if they are the same. To allow using a user-defined comparison algorithm, the Set.Make functor takes a module that specifies both the element type t and the compare function. Passing the comparison function as a higher-order parameter, as done in Array.sort, for example, would add a lot of boilerplate code. Providing set operations as a functor allows specifying the comparison function only once.
I couldn't image how the higher order function way can "add a lot of boilerplate code", as a new ocaml user this can be confusing. I've tried to look at source code like Map module, I found a funcion that calling compare like add:
letrec addxdata=functionEmpty ->
Node{l=Empty; v=x; d=data; r=Empty; h=1}
|Node{l; v; d; r; h}asm ->
let c =Ord.compare x v inif c =0thenif d == data then m elseNode{l; v=x; d=data; r; h}
elseif c <0thenlet ll = add x data l inif l == ll then m else bal ll v d r
elselet rr = add x data r inif r == rr then m else bal l v d rr
So for this case, the add function if without functor, then we have to define it like this:
letrec addxdatacmp=function
....
I'm not sure if I understand it correctly, but I guess this should be one of the rational of using functor instead of higher order function. I wish this can be elaborated in a crystal clear way because that is the point of having functor in Ocaml?
The text was updated successfully, but these errors were encountered:
Hi @geohuz, thanks for your careful reading and feedback; this is helpful.
Passing the comparison function as a higher-order parameter, as done in Array.sort, for example, would add a lot of boilerplate code.
Yes, this sentence isn't great.
I couldn't image how the higher order function way can "add a lot of boilerplate code", as a new ocaml user this can be confusing.
You are right.
I'm not sure if I understand it correctly, but I guess this should be one of the rational of using functor instead of higher order function.
Yes, you got it right. This is what a functor allows, not having to pass the comparison function (for instance) at each function call.
“Boilerplate” isn't a great term, but client code is somehow simpler/shorter as the comparison function isn't passed at each call to a functor-hosted function.
And yes, this is one of the main rationales for using functors.
Do you have suggestions on how to improve this text?
Hi @cuihtlauac, thanks for your prompt reply! My suggestion is to give an example to explain the idea, so this can happen in other functors like Set...
In the ocaml docmentation chapter functor, when I read this paragraph:
I couldn't image how the higher order function way can "add a lot of boilerplate code", as a new ocaml user this can be confusing. I've tried to look at source code like Map module, I found a funcion that calling compare like add:
So for this case, the
add
function if without functor, then we have to define it like this:I'm not sure if I understand it correctly, but I guess this should be one of the rational of using functor instead of higher order function. I wish this can be elaborated in a crystal clear way because that is the point of having functor in Ocaml?
The text was updated successfully, but these errors were encountered: