You may frequently avoid direct recursion by use of higher-order functions: pass in a function to another function, which then handles the details of iteration or aggregation using your passed in the function. Examples include:
map f xs
, which applies a function f
to each element in xs
, creating a new listList.filter p xs
, which filters a list using a predicate p
, creating a new list containing any items that returned true
for p x
foldr c s xs
lets you combine the items of a list to a summary value, using some combining operation c
and a starting value s
(* examples/hofs.sml *)