SML - unbound variable or constructor

I have the following code:

datatype expr = K of string| Number2 of expr * (expr list);
datatype number = Number1 of string | Number3 of int;
 fun append (nil, l2) = l2 
  | append (x::xs, l2) = x::append(xs, l2);
 fun map [] = []
    | map (h::t) = (What h)::(map t);
fun What (K x) = [Number1(x)]
    |What (Number2 (t,[])) = Number3(0)::What(t)
    |What (Number2 (y,a::b)) =  append(What(a), map(b));

It does not recognize the What function (unbound variable or constructor). How can I fix this so that he knows the "What" function?

Thank.

+5
source share
2 answers

SML ads work from top to bottom, so they mapdon’t see What. Switching the order will not help, because then Whatit will not see map, indicating the same error. Instead, you need to declare mutually recursive functions simultaneously with and:

fun map [] = []
  | map (h::t) = (What h)::(map t)
and What (K x) = [Number1(x)]
  | What (Number2 (t,[])) = Number3(0)::What(t)
  | What (Number2 (y,a::b)) =  append(What(a), map(b))
+7
source

and . , . What a expr -> number list, , map expr list -> (number list) list, number list list number list. , , , , , . .

+3

All Articles