MathGroup Archive 2010

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

Search the Archive

Re: newbie list question

  • To: mathgroup at
  • Subject: [mg115012] Re: newbie list question
  • From: dr DanW <dmaxwarren at>
  • Date: Tue, 28 Dec 2010 06:50:43 -0500 (EST)
  • References: <if70bt$330$>

Your first lesson in functional programming.  First, if you are
serious about learning Mathematica, you should pick up a book that
covers the fundamentals, like The Mathematica Cookbook from O'Reilly.
I've been using Mathematica for over 20 years, and I still read one of
these books every so often because there is always something I have
missed.  I would suggest reading the Doc Center, but alas it has
become so fragmented over the last few versions that it does not read
as a book (a linear introduction of concepts) which would really help
the new user.

Here are your lists:

listA = {4, 5, 8, 2, 6, 4};

listB = {8, 4, 2};

When facing a problem like this you want to build up your expression.
Only the very gifted can write an elegant Mathematica expression in
one pass.  First, consider the question "how do I find the position of
one specific element in listA?"  After reading tutorial/
TestingAndSearchingListElements, you realize that Position[] is the
function you need:

Position[listA, 4]

Out[3]= {{1}, {6}}

well, that gave you every position of 4 in listA, presented as a list
(which makes more sense if you are looking for elements in a matrix,
but is done this was for a list for consistency).  So, use Part[]
(also done as double brackets [[ ... ]] ) to extract the value you

Position[listA, 4][[1,1]]

Out[4]= 1

Great, so you can do it for one value, but you have a list.  This is
the functional programming part.  In functional languages, once you
have a function to do something once, there are functions that you can
use to work that function over elements of a list (or matrix, or more
deeply nested structures.)  In this case, you need Map[].

(Position[listA, #1][[1,1]] & ) /@ listB

Out[5]= {3, 1, 4}

Where is Map?  This is the part that throws a lot on new users, the
use of operators instead of function names looks like the cat walked
across the keyboard.  Once again, break it down. First you need to
make a Function[].  Completely spelled out, this would be

Function[{x}, Part[ Position[listA, x], 1, 1 ] ]

in operator shorthand, it becomes

(Position[listA, #1][[1,1]] & )

where the #1 indicates the first slot of the function (a generic tag
for the first argument in the function call) and the & is the operator
for Function[].  In Mathematica, operators can be placed before
(prefix) in the middle (infix) or after (postfix) the arguments.  This
sounds confusing at first, but it makes sense as you use it.  Finally,
the operator /@ is an infix operator for Map[].  Map[ list, fun ] (or
fun /@ list ) throws the function fun at every element of list and
returns a list of the results.

Practice with this.  Experiment with it in Mathematica, see what
happens when you change things in the expression, or have a more
deeply nested structure you are looking in.


  • Prev by Date: Re: Mathematica daily such and so
  • Next by Date: Re: Mathematica 8 & reports / books
  • Previous by thread: Re: newbie list question
  • Next by thread: Re: newbie list question