问题
I'm building a simple function to remove item
from List1
...
fun Strip(item, List1) = filter (fn x => x <> item) List1;
Input:
Strip(3,[1,2,3,4,3]);
Error:
Error: Unbound variable or constructor: Strip
Alternate input:
filter (fn x => x <> 5) [1,3,5,2,5];
Alternate error:
stdIn:1.2-1.8 Error: unbound variable or constructor: filter
Any ideas why such a simple function isn't working?
回答1:
As to the error message "unbound variable or constructor: filter", in this case it means that the identifier "filter" is not present in the toplevel environment. Since filter
is defined in the structure List
, you'll have to either use List.filter
, or issue the statement open List
before using filter
. For example,
List.filter (fn x => x <> 5) [1,3,5,2,5];
or
open List;
filter (fn x => x <> 5) [1,3,5,2,5];
As for the error when using Strip
, are you certain that your definition of Strip
was successful and there were no errors? If you tried using filter
like your code shows, and the interpreter couldn't find it, the definition of Strip
should have failed.
来源:https://stackoverflow.com/questions/21870602/list-filter-function-not-working-in-sml-nj