Anonymous functions defined based on "imported" values


Example Problem:

From two lists L and M, create the list of all pairs :

first component from L,

second component from M

e.g.

L = [a,b,c,d]

M = [1,2,3]

result

[[a,1],[a,2],[a,3],[b,1],[b,2],[b,3],[c,1],[c,2],º,[d,3]]

"Cartesian product"

 

How to approach:

First do for a single  element, say X, of L:

[[x,1], [x,2], [x,3]]

Use map :

map((Y) Þ [x,y], M)

           

X is "imported"; it is an otherwise unbound variable

 

Second, map a function which has X as its argument over each value of L:

This gives, for the current example,

[

 [[a,1],[a,2],[a,3]], ¨ 1st inner map

 [[b,1],[b,2],[b,3]], ¨ 2nd

 [[c,1],[c,2],[c,3]],

 [[d,1],[d,2],[d,3]]

]

 

The list above is not quite it; we want all lists to be appended  together.

mappend((x)Þ map((Y)Þ [x,y],M),L)

Could define

pairs(L,M) =  ;

Note: This problem is homework-related.

 

To Next Slide To Previous Slide To Contents