(unlike, say, rex)
There are two ways of getting the effect of a function such as map:
The second of these requires some ideas more advanced than we can present right now, so we illustrate only the first.
Wired-in version of map:
In order to achieve a map effect, refer to a possible definition of map in rex:
map(F, [ ]) => [ ];map(F, [A | L]) => [F(A) | map(F, L)];
In Java, suppose the function is a known static method. Only the list is an argument:
static IntList mapF(IntList L){if( isEmpty(L) )return IntList.nil;
return IntList.cons(F(IntList.first(L)), mapF(IntList.rest(L)));}
For example, if we wanted to square each element of a list, then F would be defined as:
static int F(int x){return x*x;}
Next Slide | Previous Slide | Contents |