Le Thursday 05 Jan 2012 à 04:05:57 (-0800), DelPhiNus a écrit :
> Hi,
>
> I would like to know if there is any way to write a nested polymorphic
> function instead of a polymorphic function.
>
> The idea is that that the nested polymorphic function capture a some
> values from the parent functions and takes generic arguments for
> various needs.
>
> Here is a very simple code but the idea is that I want to implement
> some kind of "fold" function that reads tokens and call the user
> function with "accumulator" and a value but the accumulator and result
> should be polymorphic.
>
> let rec read_richfile t =
> ...
> let rec read_richtext f a =
> match read_token t with
> | StartElement "RichRegion" -> read_richtext (f a (RichBegin))
> | EndElement "RichRegion" -> read_richtext (f a (RichEnd))
> | StartElement "Color" -> read_richtext (f a (RichColor
> (att_value_ "rgb")))
> | EndElement "RichText" -> read_richtext (f a (RichEnd))
> | _ -> read_richtext a
> in read_richtext a
This is not valid. On that last line, the identifier a escapes the scope
in which it was defined.
Moreover, the type of a on that last line would be unified with the type
of f on the declaration line of read_richtext. Not sure what you want to
achieve.
> let other_fun () =
> read_richtext (fun a b -> a :: b) []
>
> let another_fun () =
> read_richtext (print_rich_text richprinter) ()
These two are nested in 'read_richfile', aren't they? If yes, then there
is no typing error. The so-called 'let ... in' construct in ML will give
you enough polymorphism here.
However, writing something like
let x =
let f x = x
let y = f 1
let z = f ()
(which is what you seem to want to write) is syntactically invalid.
let x =
let f x = x in
let y = f 1 in
let z = f () in
()
is valid however.
> Actually I am not very keen in writing a top-level function with all
> necessary arguments each time I need to do some generic processing
> that is specific to one master function.
Not so sure what you mean by that.
> Thanks,
> Delphin
--
Guillaume Yziquel
http://yziquel.homelinux.org
--
You received this message because you are subscribed to the Google Groups
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html