Adam Sh
Adam Sh

Reputation: 8577

SML - lists and recursion

I have list in SML, which contains members from datatype "expr" (list "b"). I also have function "What", which it's arguments are only from dayatype "expr". Now I have problem in the next code:

datatype expr = K of string| Number2 of expr * (expr list);
datatype number = Number1 of string | Number3 of int;
What....
| What (Number2 (t,[]))= Number3(0)::What(t)
| What (Number2 (y,(a::b)) = append (What(a), What(b));

The error occurred because b is list of expr, and the function What got only "expr" and not "expr list". All I want to do is to check all the members is "b", and make a new list - which member is from datatype "number". I tried to use map function, but it didn't help (see the marks here: SML - unbound variable or constructor).

Any idea? There is another way to do it, without using map? I stack on it for a day..

Upvotes: 1

Views: 7353

Answers (1)

Jesper.Reenberg
Jesper.Reenberg

Reputation: 5944

For the sake of getting the question closed.

The append function you made in the previous question:

fun append (nil, l2) = l2 
  | append (x::xs, l2) = x::append(xs, l2);

can be replaced with the built in append operator @. As the documentation describes:

l1 @ l2
    returns the list that is the concatenation of l1 and l2. 

Upvotes: 9

Related Questions