0

I have the code below, and I am confused on why it is not compiling. I understood pattern matching can be used to match against different kinds of constructors, and as long as the expressions on the right side return the same type, it should be fine. But this is not happening below. Could someone please explain this?

fun myfunc (s: string, lst: string list) = 
    let fun f (s, []) = NONE
      | f (s, x::xs') = SOME ["list"]
      | f (s, SOME(x::xs')) = SOME ["some"]
    in
       f(s, lst)
    end

When trying to run this code, the compiler throws an error stating

hw2provided.sml:25.13-27.46 Error: parameter or result constraints of clauses don't agree [tycon mismatch]
  this clause:      'Z * 'Y list option -> 'X
  previous clauses:      'Z * 'W list -> 'X
  in declaration:
    filter =
      (fn (s,nil) => NONE
        | (s,:: <pat>) => SOME (<exp> :: <exp>)
        | (s,SOME <pat>) => SOME (<exp> :: <exp>))
Payam Mesgari
  • 953
  • 1
  • 19
  • 38
  • Try to figure out the type of `f` - it is `'a * T -> string list option`, for some type `T`. What is `T`? – molbdnilo Apr 17 '20 at 14:31

1 Answers1

1

The parameters must also be of the same type, but you can match on different constructors of that same type.
For instance, you can match on [] and ::, which are both list constructors, but you can't add SOME to that mix since it isn't.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82