caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* polymorphic recursion clarification
@ 2008-05-12 22:22 Jacques Le Normand
  0 siblings, 0 replies; only message in thread
From: Jacques Le Normand @ 2008-05-12 22:22 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 1041 bytes --]

maybe I should state why I need polymorphic recursion
consider the function traverse which takes a function and an element of what
I want to traverse and returns a list of results.

let rec traverse f n=
  let ret = f n in
  let rest = match n#get_right_sibling_specific with
      None -> []
    | Some y -> traverse f y
  in
  let rest2 = match n#get_child_specific with
      None -> []
    | Some y -> traverse f y
  in
    match ret with
    None -> rest @ rest2
      | Some y -> y :: rest @ rest2


with type

val traverse :
  ((< get_child_specific : 'a option; get_right_sibling_specific : 'a
option;
      .. >
    as 'a) ->
   'b option) ->
  'a -> 'b list

the problem is that get_child_specific may not return an 'a option, but it
will always return a subtype of 'a option. is there any way to do this? ie,
give it a type

((< get_child_specific : #'a option; get_right_sibling_specific : #'a
option;
      .. >
    as 'a) ->
   'b option) ->
  'a -> 'b list

? (I believe this can be done with polymorphic recursion)
--Jacques

[-- Attachment #2: Type: text/html, Size: 1546 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-05-12 22:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-12 22:22 polymorphic recursion clarification Jacques Le Normand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).