Having trouble with simple implementation of flatten in SML -
i'm trying implement flatten : 'a list list -> 'a list list in sml. thought should relatively straight forward higher order functions. implementation is
val flatten = list.reduce (op @) []
however i'm getting bizarre error message: "append.sml:1.6-1.36 warning: type vars not generalized because of value restriction instantiated dummy types (x1,x2,...)". when try flatten int list list type error:
:> flatten [[1,2],[3]];
stdin:2.1-2.20 error: operator , operand don't agree [literal]
operator domain: ?.x1 list list
operand: int list list in expression: flatten ((1 :: 2 :: nil) :: (3 :: nil) :: nil)
as error message hints, ran value restriction -- see here explanation. solution simple: "eta-expand" definition, i.e., make parameter explicit instead of relying on partial application:
fun flatten xs = list.reduce op@ [] xs
Comments
Post a Comment