MathGroup Archive 1996

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

Search the Archive

Re: Help on Function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3858] Re: [mg3799] Help on Function
  • From: brucec (Bruce Carpenter)
  • Date: Sat, 4 May 1996 02:12:52 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

>Hi people,
>
>Could you please help me? I4m trying to write a function (not procedure
>if possible) that works on a list such as {a1, a2, a3, a4, ...} and
>returns a list {a2/a1, a3/a2, a4/a3, ...}. I know it should be an easy
>task but I4m a newbe...
>
>Thanks a lot

Hi Roberto,

Here are a couple of ways to do it:

In[65]:=
myfunction1[vec_?VectorQ] := Partition[vec,2,1] /.{a_,b_}->b/a
mylist = {a1,a2,a3,a4,a5,a6};
myfunction1[mylist]

Out[66]=
{a2/a1, a3/a2, a4/a3, a5/a4, a6/a5}

In[67]:=
myfunction2[vec_?VectorQ] := Rest[mylist]/Drop[mylist,-1]
myfunction2[mylist]

Out[68]=
{a2/a1, a3/a2, a4/a3, a5/a4, a6/a5}

myfunction1 has the virtue of handling the empty list more gracefully,
while myfunction2 is faster.

Cheers,
Bruce Carpenter

-----------------------
Bruce Carpenter
Courseware Coordinator         phone: (217) 398-0700
Wolfram Research, Inc.           fax: (217) 398-0747
100 Trade Centre Drive         email: brucec at wolfram.com
Champaign, IL  61820             web:  http://www.wolfram.com



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


  • Prev by Date: Tick marks and labels
  • Next by Date: Re: Function
  • Previous by thread: Re: Help on Function
  • Next by thread: Re: Help on Function