1
votes

I want to implement the nested functions which are recursive. How to do that? I am getting an error in the following code "an operator expected"

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

Here, lst = any list (e.g [1;2;3]) f1 any characteristic function which returns true if the element is in the set defined by characteristic function. (e.g. let f1 x = (x mod 3 == 0 && x<=15);; ) The powerset_cf function returns true if the lst is the member of the power set of the characteristic function.

Please help fixing this. Thanks in advance.

1
The internal let rec should be a let rec ... in ... and you are lacking the in ... part. BTW, be sure to indent properly (for readability reasons) your code.Basile Starynkevitch

1 Answers

2
votes
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
;;

Of course we can simplify the code, but that's not the question asked.