【问题标题】:List.filter function not working in SML/NJList.filter 函数在 SML/NJ 中不起作用
【发布时间】:2014-03-19 04:47:58
【问题描述】:

我正在构建一个简单的函数来从 List1 中删除 item ...

fun Strip(item, List1) = filter (fn x => x <> item) List1;

输入:

Strip(3,[1,2,3,4,3]);

错误:

Error: Unbound variable or constructor: Strip

备用输入:

filter (fn x => x <> 5) [1,3,5,2,5];

替代错误:

stdIn:1.2-1.8 Error: unbound variable or constructor: filter

任何想法为什么这么简单的功能不起作用?

【问题讨论】:

    标签: sml smlnj ml


    【解决方案1】:

    对于错误信息“未绑定的变量或构造函数:过滤器”,在这种情况下,它意味着标识符“过滤器”不存在于顶级环境中。由于filter 是在List 结构中定义的,因此您必须使用List.filter,或者在使用filter 之前发出语句open List。例如,

    List.filter (fn x => x <> 5) [1,3,5,2,5];
    

    open List;
    filter (fn x => x <> 5) [1,3,5,2,5];
    

    至于使用Strip时出现的错误,你确定你的Strip定义成功并且没有错误吗?如果您尝试像代码所示那样使用filter,但解释器找不到它,则Strip 的定义应该失败了。

    【讨论】:

    • 我听取了您的建议并尝试了open List 以及简单地使用List.filter,但都没有奏效。 List.filter (fn x =&gt; x &lt;&gt; 5) [1,2,3,4,5]; 产生这个输出:[autoloading]unexpected exception (bug?) in SML/NJ: Io [Io: openIn failed on"C:\cygwin\home\larsberg\smlnj2\sml.boot.x86-win32\smlnj\basis\.cm\x86-win32\basis.cm", win32-bin-prim-io:checkHndl: openRd: failed]-io:checkHndl: openRd: failed]raised at: Basis/Implementation/IO/bin-io-fn.sml:617.25-617.71../cm/util/safeio.sml:30.11../compiler/TopLevel/interact/evalloop.sml:44.55
    • 通过创建文件夹 C:\cygwin\home\larsberg\smlnj2\sml.boot.x86-win34 并将基础文件复制到其中来修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2021-03-23
    • 2013-09-30
    • 2014-02-23
    • 2016-04-06
    • 2012-08-25
    • 2016-02-02
    相关资源
    最近更新 更多