IamPolaris
IamPolaris

Reputation: 1051

Why is this SML function returning a syntax error?

fun first_answer(my_f:('a -> 'b option)) : 'a list -> 'b =
let
fun help(_a:'a list) : 'a =
(((List.map valOf)o(List.filter isSome)o(List.map my_f)) _a)
in
help
end;

Error: syntax error: replacing WILD with SEMICOLON

Something with that _a is messing it up..... The error is linked to the last usage of _a

I am not getting very far, and I've rearranged the logic many ways already. As you can see the first_answer returns takes a function and returns a function. This is what I am doing here and I am following the types as far as I know. There is probably something simple that I am not seeing.

Upvotes: 0

Views: 115

Answers (1)

Andreas Rossberg
Andreas Rossberg

Reputation: 36118

It's simple indeed: an identifier cannot start with an underscore. So _a is parsed as if you had written _ a, in accordance with the usual maximal munch rule for lexical syntax.

Edit: Extra tip: Your function does not have the type 'a list -> 'b, because help returns a list of 'bs, not a single value of type 'b. Moreover, as written, it can be implemented more easily as

fun first_answer f xs = List.mapPartial f xs

or, in fact,

val first_answer = List.mapPartial

Upvotes: 4

Related Questions