How do I fix my standard ml code when I have an unbound value 'a'?

31 views Asked by At

This a function seconds that takes a list of pairs and returns a list of the first objects in the pairs.

fun seconds (pairs : (a * b) list) : b list =
  let
    fun first (pair : a * b) : a = fst pair
  in
    map first pairs
  end;

val pairs = [("m", false), ("w", false), ("n", true)];
val result = seconds pairs;

Error:

Elaboration failed: Unbound type "a"

The error I'm having is that 'a' is an unbound value.

I tried fixing the code by adding parenthesis around a or removing the a in other parts and I ended up with the same error.

1

There are 1 answers

0
Chris On

As noted in comments, type variables begin with '.

fun seconds (pairs : ('a * 'b) list) : 'b list =
  let
    fun first (pair : 'a * 'b) : 'a = fst pair
  in
    map first pairs
  end;

Of course, all of this type annotation is unnecessary. Let type inference do its work.

fun seconds pairs =
  let
    fun first pair = fst pair
  in
    map first pairs
  end;

And this is really just a very verbose way to write:

fun seconds pairs =
  map fst pairs;