COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Style Question: The Functional Way

• Email
• Follow

```Hello,

So I'm trying to learn how to do things the functional way. As an exercise
I'm trying to program a simple economics related table that gives revenue,
marginal revenue, etc given a demand schedule (2 lists of numbers
representing quantity and price respectively). A simple task in a

The point is that I need a list of the differences in the given lists, from
which I will be using to get marginal values (derivatives). I'll call it,
say, DeltaList, and I was wondering if this would be a proper "functional"

In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]

(DelatList: list -> list)

Basically, it partitions the list into ordered pairs, reverses those ordered
pairs to prep them for the subsequent Subtract application.

functional way?

Thank you. All of you have been most helpful in the past.

```
 0

See related articles to this posting

```On Jan 20, 10:27 pm, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> functional way?
>
> Thank you. All of you have been most helpful in the past.

Check out Differences

??Differences

Shouldn't you also have a third axis for time? Revenue and quantity
are dynamic.

Mike

```
 0

```Of course that function would exist. Thank you for pointing that out. :)

On Thu, Jan 20, 2011 at 4:39 AM, Bob Hanlon <hanlonr@cox.net> wrote:

> DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> data = Array[x, 10];
>
> The simplest way is to just use Differences or ListCorrelate.
>
> DeltaList[data] ==
>  Differences[data] ==
>  ListCorrelate[{-1, 1}, data] ==
>  Most[RotateLeft[data] - data] ==
>  ({-1, 1}.# & /@ Partition[data, 2, 1])
>
> True
>
>
> Bob Hanlon
>
> ---- Just A Stranger <forpeopleidontknow@gmail.com> wrote:
>
> =============
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
>
>
>
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
>
> Basically, it partitions the list into ordered pairs, reverses those
> ordered
> pairs to prep them for the subsequent Subtract application.
>
> functional way?
>
> Thank you. All of you have been most helpful in the past.
>
>
>
```
 0

```On Thu, 20 Jan 2011, Just A Stranger wrote:

> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
>
>
>
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>

Hi
here is another way

data = RandomReal[{-1, 1}, 100];
DeltaList[data] = Most[RotateLeft[data] - data]

Oliver

>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> functional way?
>
> Thank you. All of you have been most helpful in the past.
>
>

```
 0

```It's a good start.  You could also have tried DeltaList[L_] :=
Drop[RotateLeft[L] - L, -1] and probably countless others.

Of course, Mathematica already has a function that does this,
Differences.

Cheers -- Sjoerd

On Jan 20, 12:27 pm, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> functional way?
>
> Thank you. All of you have been most helpful in the past.

```
 0

```On Jan 20, 3:27 am, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an
> exercise I'm trying to program a simple economics related table
> that gives revenue, marginal revenue, etc given a demand schedule
> (2 lists of numbers representing quantity and price respectively).
>
> The point is that I need a list of the differences in the given lists,
> from which I will be using to get marginal values (derivatives). I'll
> call it, say, DeltaList, and I was wondering if this would be a proper
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those
> ordered pairs to prep them for the subsequent Subtract application.
>
> in a functional way?
>
> Thank you. All of you have been most helpful in the past.

change all the signs after subtracting:

DeltaList2[L_] := - Subtract @@@ Partition[L, 2, 1]

Instead of partitioning, work with the entire vectors, lagged:

DeltaList3[L_] := Rest@L - Most@L

Or remember that there is already a built-in routine:

DeltaList4[L_] := Differences[L]

```
 0

```DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]

data = Array[x, 10];

The simplest way is to just use Differences or ListCorrelate.

DeltaList[data] ==
Differences[data] ==
ListCorrelate[{-1, 1}, data] ==
Most[RotateLeft[data] - data] ==
({-1, 1}.# & /@ Partition[data, 2, 1])

True

Bob Hanlon

---- Just A Stranger <forpeopleidontknow@gmail.com> wrote:

=============
Hello,

So I'm trying to learn how to do things the functional way. As an exercise
I'm trying to program a simple economics related table that gives revenue,
marginal revenue, etc given a demand schedule (2 lists of numbers
representing quantity and price respectively). A simple task in a

The point is that I need a list of the differences in the given lists, from
which I will be using to get marginal values (derivatives). I'll call it,
say, DeltaList, and I was wondering if this would be a proper "functional"

In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]

(DelatList: list -> list)

Basically, it partitions the list into ordered pairs, reverses those ordered
pairs to prep them for the subsequent Subtract application.

functional way?

Thank you. All of you have been most helpful in the past.

```
 0

6 Replies
96 Views

Similar Articles

12/12/2013 4:29:13 AM
page loaded in 65657 ms. (0)

Similar Artilces:

Hi all could anyone here tell me the difference between (function(){ })(); and (function(){ )()); Thank you. Steve QQ wrote: > Hi all > > could anyone here tell me the difference between > (function(){ > > })(); > > and > > (function(){ > > )()); > > Thank you. > Steve The second one isn't valid. That's the difference. You can see the parentheses/braces aren't paired correctly. QQ wrote: > Hi all > > could anyone here tell me the difference between > (function(){ > ...

Questions about anonymous functions and classes/functions declarations
Hi guys, I'm planning to try to implemente a programming language. I got a lot of ideas, and I know how to implements them except for anonymous functions. Where I have to store them?? I have to have a table only for anonymous functions?? Someone asks to me to treat them as constants, but I can't understanda how to develop it. Now there is another questions: I'd like to give the users the ability to edit a class definition at runtime. For example: test = class() input value if value == 10 test.a_new_method = function(a) print a else test.a_new_method = function(a) print &...

function question
I have this tested code and I am wondering if it is doing what it is supposed to be doing. Have I made any errors? errno returns 0 and the response to the shell is 0. Has the char *data parameter of this function been crypted? #include <stdio.h> #include <rpc/des_crypt.h> #include <errno.h> int main() { char data[8] = "hello"; char vec[8] = "vector"; char key[8] = "encrypt"; int c; errno = 0; des_setparity(key); c = cbc_crypt(key, data, 8, DES_ENCRYPT | DES_SW, vec); printf("%d\n%d\n", c, errno);...

How to know the inline-function was implemented the inline way or normal way?
Hello All, Inline before a function definition is just a request to the compiler to make the function inline. The compiler may or maynot make it inline.. My question is ..is there any way by which I can find at runtime whether the particular function which is marked as inline,is made inline or is treated like other function by the compiler ? Thanks and Regards, Yogesh Joshi [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] ypjofficial@indiatimes.com wrote: > Inline before a function definition is jus...

Style question
I have the following line: remoteMD5hash = Socket.recv(256) But what I really want is remoteMD5hash.strip() What's the best way to say that on one line? remoteKD5hash = string.strip(Socket.recv(256)) ? Or something better? -Kamus -- o__ | If you're old, eat right and ride a decent bike. ,>/'_ | Q. (_)\(_) | Usenet posting` Kamus of Kadizhar wrote: > I have the following line: > > remoteMD5hash = Socket.recv(256) > > But what I really want is remoteMD5h...

a question of style
If I have an "add" method that adds a pointer to a foo to a private std::list, should I define add like 1. void add( const foo &f ) { lst.push_back( &f ); } to keep pointers out of my interface, or should I make it implicit that a pointer is being added to the list, like so 2. void add( foo *f ) { lst.push_back( f ); } Would the first method lead the user to believe that their foo is actually being stored, potentially leading to a bad memory value in the list once the foo is destroyed? Or is this discussion moot as long as I use pre and post conditions? Tim Partridge &...

financial functions question
Hi. These are the financial functions available in FM's help file: FV (Future Value) The future value of an investment, based on a constant interest rate and payment amount for the period NPV (Net Present Value) The net present value of a series of unequal amounts made at regular intervals, assuming a fixed interest rate per interval PMT (Payment) The payment required to meet the requirements of the term, interest rate, and principal supplied PV (Present Value) The present value of a series of equal payments made at regular intervals (periods), assuming a fixed interest rate per inter...

Pointer to function question
Hello, I'm trying to put a pointer-to-member-funtion in an std::map, but can't make it work. typedef void * (*processStringFn) (std::string * ps); typedef std::map<std::string, processStringFn> SchemaProcessorMap; class NuclearMeltdown { public: NuclearMeltdown() { std::string s("Hey"); SchemaProcessorMap map; map.insert(SchemaProcessorMap::value_type(s, NuclearMeltdown::processFn)); } virtual ~NuclearMeltdown(); SchemaProcessorMap m_map; private: void * processFn(std::string * ps) { return 0; } }; c:...

\$sql = "DELETE FROM " . \$mosConfig_dbprefix . "foo WHERE foo_id = '" . mosGetParam(\$_POST, 'cid', '' )[\$a] . "'"; was throwing errors and I fixed it be doing this: \$cidx = mosGetParam( \$_POST, 'cid', '' ); \$sql = "DELETE FROM " . \$mosConfig_dbprefix . "foo WHERE foo_id = '" . \$cidx[\$a] . "'"; My question is why didn't the first version work and is there a way to code it without adding the intermediate variable? On Jun 28, 2:41 pm, tritone <mike...@gmail.com> wrote: > ...

Mod function question
I have a series of numbers from 1.001 to 1.999. I need to determine which are odd and which are even. I understand the Mod function does this but so far I've not been able to get it to work on the numbers to the right of the decimal point. Is there some other route I need to pursue? TIA Tom Street By definition, an odd number is an integer not evenly divisible by 2. So technically, all of the values are odd. I am guessing you want to know the numbers, in which the fractional part ends in an even number. In this case, why not multiply by 1000 first? Mod ( n * 1000; 2) equals 1 in t...

Open() function question
I am having trouble using the open() function to open a sas file for update. The following code works fine with an open mode of 'I' for input only but if I try to open the file with 'U' for update I get the error: "Invalid or inconsistent mode flags for WORK.TEST1.DATA". Can someone tell me what is wrong please? data WORK.TEST1; a=23; output; run; %*let dsid=%sysfunc(open(WORK.TEST1,i)); %let dsid=%sysfunc(open(WORK.TEST1,u)); %put DSID=&dsid %sysfunc(sysmsg()); Clive Granger wrote: > I am having trouble using the open() function to open a sas fil...

Log4j functionality question
Can I get the following from Log4j? 1) Fixed size file which wraps 2) Indexed search e.g. date range 3) Custom field definition thanks Tim ...

Function structure question
I am trying to determine how to handle some variables. Here is the basic layout of my program function1{ define some constants Loop FunctionCalc{} end FunctionCalc{ do some calcs }(end FunctionCalc) FunctionDisplay{ display stuff that was calcualted {(end FunctionDisplay) }(end function1) The program works if I get rid of all the sub functions, but I dont want to loop thru the display stuff everytime. I want to do my calcs, then call the display function to only display a certain set of variables. Now of course, setup the way I have it above The Display Function doesn'...

I suppose that the targeted sub-string "@46\$1" could be found in the string "@46\$10445A50458742", but it is not. It confused me a lot. Codes as below, data _null_; Located_Value="@46\$10445A50458742"; tmp_a="@46\$"||trim(left(1)); tmp_b=index(Located_Value,tmp_a); tmp_c=length(tmp_a); tmp_d=tmp_b+tmp_c; tmp_e=substr(Located_Value,tmp_d); put Located_Value= tmp_a= tmp_b= tmp_c= tmp_d= tmp_e=; run; Here is the log, NOTE: Numeric values have been converted to character va...

Quick function Question
As long as I keep my variables within my function bounds, I should be able to use an mfile for my variables? Also if I don't want to call any matlab function ,should I just use the command window ? That or would use an mfile be easier ? On Oct 24, 4:39 pm, "John " <someonesemail2...@yahoo.com> wrote: > As long as I keep my variables within my function bounds, > I should be able to use an mfile for my variables? What do you mean by "use an mfile for my variables?" What do you mean by "keep my variables within my function bounds"? Can you gi...

Procedure/Function question
Hi All! Someone could help me in finding the answer to the following question: An oracle procedure/function can return a resultset (but not as a cursor) to the client application(ADO, ADO.NET) ? ernest "Ernest Morariu" <ernest@gesora.com> wrote in message news:<cfa2te\$lts\$1@carabinieri.cs.interbusiness.it>... > Hi All! > > Someone could help me in finding the answer to the following question: > > An oracle procedure/function can return a resultset (but not as a cursor) > to > the client application(ADO, ADO.NET) ? > > ernest a proced...