处理SML数据类型中的匿名函数

时间:2013-02-10 19:48:43

标签: types sml smlnj currying

我有以下数据类型和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上的绳索,但我无法弄清楚如何将其中一个测试称为“递归currying函数”。我试过以下功能但当然不行。有人有任何提示吗?

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"]

1 个答案:

答案 0 :(得分:1)

我假设您希望过滤给定输入传递的测试名称。

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

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