Previous Up Next

5.38.32  Apply a function of one variable to the elements of a list : map apply of

map or apply or of applies a function to a list of elements.
of is the prefixed function equivalent to the parenthesis : Xcas translates f(x) internally to of(f,x). It is more natural to call map or apply than of. Be careful with the order of arguments (that is required for compatibility reasons).
Note that apply returns a list ([]) even if the second argument is not a list.
Input :

apply(x->x^2,[3,5,1])

or :

of(x->x^2,[3,5,1])

or :

map([3,5,1],x->x^2)

or first define the function h(x)=x2, input :

h(x):=x^2

then :

apply(h,[3,5,1])

or :

of(h,[3,5,1])

or :

map([3,5,1],h)

Output :

[9,25,1]

Next example, define the function g(x)=[x,x2,x3], input :

g:=(x)->[x,x^2,x^3]

then :

apply(g,[3,5,1])

or :

of(g,[3,5,1])

or :

map([3,5,1],g)

Output :

[[3,9,27],[5,25,125],[1,1,1]]

Warning!!! first purge x if x is not symbolic.
Note that if l1,l2,l3 are lists sizes([l1,l2,l3]) is equivalent to map(size,[l1,l2,l3]).


Previous Up Next