MathGroup Archive 2007

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

Search the Archive

Re: conditionals on lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79118] Re: conditionals on lists
  • From: Peter Pein <petsie at dordos.net>
  • Date: Thu, 19 Jul 2007 03:27:23 -0400 (EDT)
  • References: <f7ke4t$4pf$1@smc.vnet.net>

mausgeo at aol.com schrieb:
> I am trying to run a simulation of a roulette game and I have created
> a list of 1's and -1's to represent wins and losses. Using the Split
> command I can get ordered pairs with the first coordinate being either
> 1 or -1 and the second coordinate being the number of successive
> entries. I am having trouble applying a conditional to this new list
> as I would like to perform a different function depending on the first
> coordinate of the ordered pair. How do I apply a conditional to a list
> of ordered pairs?
> 
> 

Hi,

the first method, which comes to my mind is using patterns.

Say you've got a list
In[1]:=
data = {{1, 3}, {-1, 17}, {1, 1}};

and now do a Replace (/.):
In[2]:=
data /. {{1, n_} :> winfunc[n], {-1, n_} :> losefunc[n]}
Out[2]=
{winfunc[3], losefunc[17], winfunc[1]}

you can use Switch[] (with "error-handling"):
In[3]:=
winloss[wl_, n_] :=
  Switch[wl,
     1, winfunc[n],
    -1, losefunc[n],
     _, oops[{wl, n}]
]
In[4]:=
winloss @@@ data
Out[4]=
{winfunc[3], losefunc[17], winfunc[1]}

hth,
Peter


  • Prev by Date: Re: Style of inserted Text via Drawing Tools
  • Next by Date: Re: fast way of appending x zeros to a list?
  • Previous by thread: Re: conditionals on lists
  • Next by thread: generating non-IID random sequences