0

I have the following datatype and 3 examples of tests:

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");

I'm still learning the ropes on SML, but I can't figure out how to "call" one of the tests as a recursive currying function. I tried with the following function but of course it wouldn't work. Anyone have any tips?

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

1 回答 1

1

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

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

fun pass x [] = []
  | pass x (Test(f, s)::t) = if f x then s::pass x t 
                             else pass x t
于 2013-02-10T20:30:47.950 回答