【问题标题】:Handling anonymous functions in SML datatypes处理 SML 数据类型中的匿名函数
【发布时间】:2013-01-25 22:47:12
【问题描述】:

我有以下数据类型和 3 个测试示例:

datatype 'a test = Test of ('a -> bool) * string;
val pos = Test (fn x => x > 0, "pos");
val even = Test (fn x => x mod 2 = 0, "even");
val small = Test (fn x => x < 100, "small");

我仍在学习 SML 的技巧,但我不知道如何将其中一个测试“调用”为递归柯里化函数。我尝试了以下功能,但当然它不起作用。有人有什么建议吗?

fun pass x [] = []
    | pass x (h::t) = (h x)::(pass x t);

pass: 'a -> 'a test list -> string list; 
i.e. pass' ~101 [pos, even, small] = ["small"]

【问题讨论】:

    标签: types sml smlnj currying


    【解决方案1】:

    我假设您想过滤给定输入通过的测试名称。

    您可以通过模式匹配分解'a test,获取相应的函数并在当前输入上测试它们:

    fun pass x [] = []
      | pass x (Test(f, s)::t) = if f x then s::pass x t 
                                 else pass x t
    

    【讨论】:

      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 2019-11-28
      相关资源
      最近更新 更多