| Author |
Message |
< Erlang ~ Calculate difference between two adjacent list elements |
| niglas |
Posted: Wed Feb 02, 2011 2:21 pm |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
How do I calculate the difference of two adjacent sublists?
From:
Code:
[[51,"one"],
[55,"two"],
[57,"three"]]
To:
Code:
[[0,"one"],
[4,"two"],
[2,"three"]]
|
|
|
| Back to top |
|
| zajda |
Posted: Wed Feb 02, 2011 8:26 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
This is without std lib, however it would be possible to use mapfoldl.
Code: -module(xxx).
-export([diff/3, diff/1]).
diff(Input) ->
diff(Input, 0, []).
diff([[CurrentX, CurrentVal]|Tail], 0, []) ->
diff(Tail, CurrentX, [[0, CurrentVal]]);
diff([], _PrevX, Acc) ->
lists:reverse(Acc);
diff([[CurrentX, CurrentVal]|Tail], PrevX, Acc) ->
diff(Tail, CurrentX, [[CurrentX - PrevX, CurrentVal]|Acc]). |
|
|
| Back to top |
|
| koops.j@gmail.com |
Posted: Thu Feb 03, 2011 12:02 pm |
|
|
|
User
Joined: 31 Jan 2009
Posts: 12
|
As suggested by zajda, a solution using lists:mapfoldl/3:
Code:
diff(L) ->
{ Result, _ } =
lists:mapfoldl(fun([ C, T ], nil) -> { [ 0, T ], C };
([C, T], Prev) -> { [ C-Prev, T ], C }
end, nil, L),
Result.
|
|
|
| Back to top |
|
| niglas |
Posted: Fri Feb 04, 2011 9:00 am |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
Thanks for the all help.
I'm facing another similar problem too. It seems a little bit more tricky because it requires look ahead instead:
From:
Code:
[[51,"one"],
[55,"two"],
[57,"three"]]
To:
Code:
[[4,"one"],
[2,"two"],
[0,"three"]]
My first thought is that it could be achieved by reversing the list before putting it into the list, but is there a better way? |
|
|
| Back to top |
|
| rvirding |
Posted: Sat Feb 05, 2011 5:41 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
You need to do some lookahead here:
Code: diff ([[X1,V1],[X2,V2]=Two|Rest]) ->
[[X2-X1,V1]|diff([Two|Rest])];
diff([[_,V]]) -> [[0,V]].
Using the alias in the pattern, =, means we can have our cake an eat by both pulling apart the second element and keeping a reference to it, i.e. it is a bit more efficient. We could do it for the whole rest of the list after the 1st element but it becomes a bit more difficult to read:
Code: diff([[X1,V1]|[[X2,V2]|_]=Rest]) ->
[[X2-X1]|diff(Rest)];
Code not tested and hopefully I didn't misunderstand what you wanted. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| niglas |
Posted: Tue Feb 08, 2011 11:01 am |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
I think I like the second one better.
The final function I used is then:
Code: diff([[X1,V1]|[[X2,_]|_]=Rem]) ->
[[X2-X1]|diff(Rem)];
diff([[_,V]]) -> [[0,V]]. |
|
|
| Back to top |
|
| rvirding |
Posted: Tue Feb 08, 2011 11:19 am |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
Sorry, a mistake crept into my code! It should be:
Code: diff([[X1,V]|[[X2,_]|_]=Rem]) ->
[[X2-X1,V]|diff(Rem)];
diff([[_,V]]) -> [[0,V]].
I forgot to add the actual value not just the difference. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| niglas |
Posted: Tue Feb 08, 2011 11:46 am |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
rvirding wrote: It should be:
Code: diff([[X1,V]|[[X2,_]|_]=Rem]) ->
[[X2-X1,V]|diff(Rem)];
diff([[_,V]]) -> [[0,V]].
Yeah, I made a typo too. It should have been what you now corrected it to. That's what I am using right now. Thanks! |
|
|
| Back to top |
|
| wuji |
Posted: Wed Aug 15, 2012 6:30 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
will always be remembered for her intelligence, beauty, compassion, humor humor cheap Ralph Lauren humor and her kind heart," her family wrote in her
Secret Addiction: Stepping Out of Adderall's ShadowBetween 2002 and 2010, 2010, designer replica *beep* 2010, a 750 Percent Increase in Adderall Prescriptions for
26 to 39By DAN HARRIS and LANA ZAKJune 26, 2012 2012 cheap Ralph Lauren Polo 2012 All over America there are moms in the shadows
in their daily responsibilities and turning to the prescription drug drug replica designer *beep* drug Adderall for relief.Adderall is a drug for attention deficit
disorder, but these women don't have ADHD; they say they they [h2]cheap Ralph Lauren Polo[/h2] they need Adderall to be better mothers.Between 2002 and 2010,
been a 750 percent increase in Adderall prescriptions for women women [h3]discount designer *beep*[/h3] women between 26 and 39. Critics say clearly not all |
|
|
| Back to top |
|
| wuji |
Posted: Wed Aug 15, 2012 6:30 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
will always be remembered for her intelligence, beauty, compassion, humor humor cheap Ralph Lauren humor and her kind heart," her family wrote in her
Secret Addiction: Stepping Out of Adderall's ShadowBetween 2002 and 2010, 2010, designer replica *beep* 2010, a 750 Percent Increase in Adderall Prescriptions for
26 to 39By DAN HARRIS and LANA ZAKJune 26, 2012 2012 cheap Ralph Lauren Polo 2012 All over America there are moms in the shadows
in their daily responsibilities and turning to the prescription drug drug replica designer *beep* drug Adderall for relief.Adderall is a drug for attention deficit
disorder, but these women don't have ADHD; they say they they [h2]cheap Ralph Lauren Polo[/h2] they need Adderall to be better mothers.Between 2002 and 2010,
been a 750 percent increase in Adderall prescriptions for women women [h3]discount designer *beep*[/h3] women between 26 and 39. Critics say clearly not all |
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 5:25 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|