First of all, with foldr
you should be looking for a 1-line definition rather than a recursive definition using patterns and cases. The point of foldr
is that it incorporates a common recursion pattern -- you just need to use the pattern.
The type of foldr
is
fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
In your case 'b
is int list
and 'a
is int
. The 'b
between the arrows in the middle of foldr
's type is the seed value. It typically corresponds to a basis value. When you are constructing lists this basis value is typically []
. Thus -- you need to concentrate on the key question of what should be folded over the list. In other words -- what function of type ('a * 'b -> 'b)
should you pass to foldr
? In your case you need to pass a function of type
int * int list -> int list
this should be a function which, when given an int and an int list either tacks the int onto the list (if it is even) or leaves the list alone. You could define this function ahead of time, define it using let
, or just use an anonymous function.
Under the assumption that this is homework, I don't want to give a complete answer, but here is a function which uses foldr
to obtain the positive entries in a list:
fun positives xs =
foldr (fn (x,xs) => if x >= 0 then x::xs else xs) [] xs;
-
- positives [3,~2,4,5,0,~1,~1,5];
val it = [3,4,5,0,5] : int list