OCaml - 实现嵌套函数

时间:2016-08-29 12:00:09

标签: ocaml

我想实现递归的嵌套函数。怎么做? 我在下面的代码“运算符预期”中收到错误

let powerset_cf f1 = if (let rec f lst = match lst with 
                                  [] -> true
                                  | h::t ->  if (f1 h) then (f t) 
                                             else false ) then true
                     else false;;

下面,       lst =任何列表(例如[1; 2; 3])       f1任何特征函数,如果元素在特征函数定义的集合中,则返回true。 (例如,让f1 x =(x mod 3 == 0&& x< = 15);;) 如果lst是特征函数的幂集的成员,则powerset_cf函数返回true。

请帮忙解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

let powerset_cf f1 lst =
  let rec f lst = 
    match lst with
      | []   -> true
      | h::t ->  if f1 h then f t else false
  in
  if f lst then true else false
;;

当然,我们可以简化代码,但这不是问题。

相关问题