I'm trying to create a length function, similar to the one already included in ML. My restrictions are that it has to be done on one line and use either map, foldl, or foldr.
Right now my line of code looks like this:
val mylength = foldr ( fn(x,y) => 1+y) 0;
I am by no means an expert at ML, but my reasoning so far is this:
To my understanding, foldr will, beginning at the last item in the list, pass it as the x argument in my function and use the 0 as the initial y value. It should then add 1 to the y value and basically ignore x. In theory, I believed this would give me my total length. However I am given the following error:
stdIn:136.5-136.37 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
val mylength = fn : ?.X1 list -> int
My big problem is figuring out how to create this function in a way that it can accept lists of any type.
If anyone could offer some advice about how to approach this problem I would appreciate it, perhaps I still haven't wrapped my head around ML's style of programming.