Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to think about Map[ ] ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4693] Re: How to think about Map[ ] ?
  • From: von_Aschen at uni-duisburg.de (Harald von Aschen)
  • Date: Sun, 25 Aug 1996 18:23:18 -0400
  • Organization: Gerhard-Mercator-Universitaet GHS Duisburg Germany
  • Sender: owner-wri-mathgroup at wolfram.com

AES <siegman at ee.stanford.edu> wrote:

> I can understand that  
> 
>    Map[f,{a,b,c}] --> {f[a], f[b], f[c]}
> 
> But would someone want to give a little tutorial on how to understand
> the (what seem to me) bizarre results I get when I try various
> combinations like
> 
>    Map[f, a + b + c]
> 
>    Map[f, a + b - c]
> 
>    Map[f, a * b * c]
> 
>    Map[f, a / b / c]
> 
> and other more complex forms. 


Try

In[1]:=   ?? Map
Out[1]=   Map[f, expr] or f /@ expr applies f to
          each element on the first level in expr.
          Map[f, expr, levelspec] applies f to
          parts of expr specified by levelspec.

          Attributes[Map] = {Protected}
          Options[Map] = {Heads -> False}

The trick how you can see how Map would work is to see the FullForm of
an Expresseion:

In[2]:=   FullForm[{a,b,c}]
Out[2]=   List[a, b, c]

Map now applies f to each Element of List:

In[3]:=   Map[f, {a,b,c}]
Out[3]=   {f[a], f[b], f[c]}

If you look on a + b + c FullForm gives:

In[4]:=   FullForm[a + b + c]
Out[4]=   Plus[a, b, c]

This means Map [f, a + b + c] would give Plus[f[a], f[b], f[c]]:

In[5]:=   Map[f, a + b + c]
Out[5]=   f[a] + f[b] + f[c]

Going further on with FullForm you can understand more complicated
expressions like

In[6]:=   Map[ f, g[{a,b,c}] ]
Out[6]=   g[f[{a,b,c}]]

because

In[7]:=   FullForm[ g[{a,b,c}] ]
Out[7]=   g[List[a, b, c]]

and Map applies f to List (this means: f[List[a,b,c]]).

You can apply f to more than the first level:

In[8]:=   Map[f, g[{a, b, {c, d}}], 2]
Out[8]=   g[f[{f[a], f[b], f[{c, d}]}]]

In[9]:=   Map[f, g[{a, b, {c, d}}], 3]
Out[9]=   g[f[{f[a], f[b], f[{f[c], f[d]}]}]]


If you want to learn more about these Programming features of
Mathematica (and tricks like using FullForm) of Mathematica I would like
to recommend you the book
   Programming in Mathematica, 2nd edition
   Roman Maeder
   Addison-Wesley Publishing Company
which has helped me a lot.


Kind regards, Harald
-- 
Harald von Aschen
eMail: von_aschen at uni-duisburg.de

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Help: Professional vd student versions of Mma 2.2.3
  • Next by Date: can an "array" start with an index other than 1?
  • Previous by thread: Re: How to think about Map[ ] ?
  • Next by thread: How to change multi-valued faunctions