Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

map function with unit type output

type ('a, 'b) t=
  | Leaf of 'b
  | Node of 'a * ('a, 'b) t* ('a, 'b) t
              

I want to implement a map f g t function with signature: val map: (('a -> unit) * ('b -> unit)) -> ('a,'b) t -> unit

where:

f to all node data
g to all leaf data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and it should iterate via preorder traversal (root-left-right)

This is what I have tried:

let rec map f g t = 
  match t with
  | Leaf(w)  -> Leaf(g w)
  | Node (v, l, r) -> Node (f v, map f g l, map f g r)

But I don’t know how to get that signature matched, any ideas?

>Solution :

If you want f and g to each have a return type of unit, then this isn’t really map at all. It’s more of an iter function, and you might write it more like:

let rec iter f g =
  function
  | Leaf v -> let () = g v in ()
  | Node (v, l, r) ->
    let () = f v in
    let () = iter f g l in
    iter f g r

This function has the following type because we have provided enough information to the compiler to infer that f and g return unit.

('a -> unit) -> ('b -> unit) -> ('a, 'b) t -> unit

Note that this also guarantees that the recursive call on the left branch will occur first, whereas evaluation order in Node (f v, map f g l, map f g r) may change between implementations.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading