In F#, a tail recursive List.filter
function can be implemented like:
let filter f =
let rec filterImpl f acc = function
| [] -> List.rev acc
| h :: t -> filterImpl f (if f h then h :: acc else acc) t
filterImpl f []
But it can also be implemented by a closure (i.e. without passing f
explicitly into filterImpl
)
let filter f =
let rec filterImpl acc = function
| [] -> List.rev acc
| h :: t -> filterImpl (if f h then h :: acc else acc) t
filterImpl []
What is the difference and which is preferred in F# or functional programming in general?