Re: Define function using lists or tables
- To: mathgroup at smc.vnet.net
 - Subject: [mg131436] Re: Define function using lists or tables
 - From: Alexei Boulbitch <Alexei.Boulbitch at iee.lu>
 - Date: Sat, 6 Jul 2013 05:02:46 -0400 (EDT)
 - Delivered-to: l-mathgroup@mail-archive0.wolfram.com
 - Delivered-to: l-mathgroup@wolfram.com
 - Delivered-to: mathgroup-outx@smc.vnet.net
 - Delivered-to: mathgroup-newsendx@smc.vnet.net
 
Hello. Possibly this is a very simple question.
Is there a way to define a function over a (BIG) list of ordered pairs? I mean, suppose that I have a list, created using a Table command or something like that, of the form [{a,b},{c,d},etc.] such that, after naming it f, say, f = [{a,b},{c,d},etc.], then, afterwards, whenever there is a command such that one needs to compute f[a] (or f[c]), the program immediately replaces f[a] by b,and f[c] by d?
Thank you.
Hi, Joaquim,
One way may be like the following. This is a list to play with:
lst = Partition[Map[ToExpression, CharacterRange["a", "z"]], 2]
{{a, b}, {c, d}, {e, f}, {g, h}, {i, j}, {k, l}, {m, n}, {o, p}, {q,
  r}, {s, t}, {u, v}, {w, x}, {y, z}}
Let us take, for example, o and try with it. Its position is
Position[lst, o]
{{8, 1}}
We only need the position of the corresponding pair:
Position[lst, o][[1, 1]]
8
Then the second pair member, p, has the coordinate [[8,2]]:
lst[[Position[lst, o][[1, 1]], 2]]
p
Now we can write a function doing all this:
f[lst_, x_] := lst[[Position[lst, x][[1, 1]], 2]]
 Let us try it:
Map[f[lst, #] &, {a, e, i, w}]
{b, f, j, x}
Have fun, Alexei
Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG
Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44
e-mail: alexei.boulbitch at iee.lu