Discussion:
Parametric Datatype
(too old to reply)
IL CAPITANO
2011-02-20 17:55:45 UTC
Permalink
hi all
i have this datatype
datatype tree = leaf of int | node of int*tree*tree; and a function
f:int->int

i write this function convert for apply f at all elemento of tree

fun convert(f,leaf(x))=leaf(f(x)) |
convert(f,node(x,t1,t2))= node(f(x),convert(f,t1),convert(f,t2));

how can write a convert for that Parametric Datatype
datatype ’a treeP = Empty | Node of ’a treeP * ’a * ’a treeP

i tryed this but i m not sure why i get problem whit no leaf
fun convert(f,Empty)=raise except |
convert(f,node(t1,x,t2))= node(convert(f,t1),f(x),convert(f,t2));

how can write a convert for that ’a treeP Parametric Datatype
Lauri Alanko
2011-03-05 21:18:18 UTC
Permalink
Post by IL CAPITANO
how can write a convert for that Parametric Datatype
datatype ’a treeP = Empty | Node of ’a treeP * ’a * ’a treeP
i tryed this but i m not sure why i get problem whit no leaf
fun convert(f,Empty)=raise except |
convert(f,node(t1,x,t2))= node(convert(f,t1),f(x),convert(f,t2));
If you raise an exception for an Empty tree, then you raise an
exception for every tree, since every tree has an Empty subtree.
So instead of raising an exception, return a sensible value.

Also, note that "Node" and "node" are two different identifiers in
SML. The constructor for treeP is written "Node" above.


Lauri

Loading...