List.filter function not working in SML/NJ

你离开我真会死。 提交于 2020-01-01 19:48:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!