So I wrote a function “filter” that is applied to the given predicate to each element in the input list and checks whether it should be included in the output.
let rec filer (pred: 'a -> bool) (l: 'a list) : 'a list =
begin match l with
| [] -> []
| hd :: rest -> if (pred hd) then hd :: (filter pred rest) else filter pred rest
end
Now I’m writing a predicate that can be passed to filter to keep just multiples of five.
let multiples_of_five_pred: int -> bool =
filter (fun (x: int) -> x mod 5)
I don’t know what I’m missing here…I’m getting a syntax error that says “this expression has type int but an expression was expected of type bool”.
The function given to
filterneeds to produce an boolean, but as the error message is telling you, the function you give tofilterproduces an integer, not a boolean. You need to change it to produce a boolean instead.Once you fix that, you’ll have another type error: Applying
filterto a function that takes an integer will produce a function of typeint list -> int list, but according to your type signature, you want the typeint -> bool.Also note that you say that
multiples_of_five_prodshould be a function that is given tofilter, but you actually callfilterinside the function. That doesn’t fit. You should remove the call tofilter.