| Author |
Message |
< Erlang ~ for loop in erlang |
| adamprisa |
Posted: Fri Nov 21, 2008 2:17 pm |
|
|
|
Joined: 21 Nov 2008
Posts: 7
|
I am a java programmer and want to write a for loop in erlang which will have a variable loop size and a variable increment.
Since there are no loops in erlang, can somebody help by writing and equivalent code in erlang for the below written code.
Code: int variable_length = 20;
int variable_increment = 5;
for (int i=0; i<variable_length; i=i+variable_increment)
{
int returnValue = someFunction();
}
int someFunction()
{
return 1;
}
Thanks in advance. |
|
|
| Back to top |
|
| mhenoch |
Posted: Fri Nov 21, 2008 3:06 pm |
|
|
|
User
Joined: 06 Nov 2008
Posts: 29
|
Something like this:
Code:
VariableLength = 20,
VariableIncrement = 5,
lists:map(fun(I) -> do_something_with(I) end,
lists:seq(0, VariableLength, VariableIncrement)).
But often the problem is better solved in another way. Have a look at the lists module for other useful functions. |
|
|
| Back to top |
|
| Mazen |
Posted: Fri Nov 21, 2008 7:10 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Sounds strange to use lists:seq/3 to iterate solve this. Imagine VariableLength = 1000000000, then you would first create a list of 1000000000 elements and then iterate over them. Not very good idea.
A better way would be to do a tail recursive function which passes the increment in each loop. If you want to save the results then send an accumulator along. This way you don't have twice the run time (One pass for building and one for iterating the list)
Consider the following:
Code:
for_loop(N, Length, _Inc, Acc) when N >= Length ->
io:format("Done!~n"),
lists:reverse(Acc); %% Not as bad as it sounds
for_loop(N, Length, Inc, Acc) ->
for_loop(N+Inc, Length, Inc, [do_something()|Acc]);
However if you already have the list and want to manipulate it then mhenoch's use of lists:map/2 is favoured (if you want to keep the list) or lists:foldl/3 if you want to accumulate a result.
Anyway... my 2 cents
mhenoch wrote: Something like this:
Code:
VariableLength = 20,
VariableIncrement = 5,
lists:map(fun(I) -> do_something_with(I) end,
lists:seq(0, VariableLength, VariableIncrement)).
But often the problem is better solved in another way. Have a look at the lists module for other useful functions. |
|
|
| Back to top |
|
| adamprisa |
Posted: Sat Nov 22, 2008 6:39 am |
|
|
|
Joined: 21 Nov 2008
Posts: 7
|
Thanks Mazen and Mhenoch. I really appreciate the help. With a little tweaking(changed the guard condition from N to N+1), i was able to achieve what i wanted to.
Posting my code for people who might want to implement a for in future
Code: -module(test_for).
-export([for_loop/4,go/0,foo/0]).
go()->
io:format("This will be printed~n").
foo()->
for_loop(1,15,3,[]),
ok.
for_loop(N, Length, _Inc, Acc) when N+1 >= Length ->
io:format("Done!~n"),
lists:reverse(Acc);
for_loop(N, Length, Inc, Acc) ->
for_loop(N+Inc, Length, Inc, [go()|Acc]). |
|
|
| Back to top |
|
| klaar |
Posted: Sat Dec 06, 2008 12:35 pm |
|
|
|
User
Joined: 06 Oct 2008
Posts: 11
Location: Göteborg/Sweden
|
I congratulate you upon this premature optimization.
You can also use lists:foreach/2 instead of lists:map/2, if you need to ensure that a set of side effects occur in order. |
|
|
| Back to top |
|
| Mazen |
Posted: Sun Dec 07, 2008 9:22 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
I think this had little to do with premature optimization per se... it was all about how to reason and think. A for-loop should almost not be used in functional programming, I have maybe used a for-loop kindof thingy (much like the one just posted) once... the rest of the time it comes down to iterating (or "eating" up) a list of items.
Given this then the initial suggestion with lists:seq/2 was conceptually acceptable but used for evil purposes
klaar wrote: I congratulate you upon this premature optimization.
You can also use lists:foreach/2 instead of lists:map/2, if you need to ensure that a set of side effects occur in order. |
|
|
| Back to top |
|
| klaar |
Posted: Sun Dec 07, 2008 1:01 pm |
|
|
|
User
Joined: 06 Oct 2008
Posts: 11
Location: Göteborg/Sweden
|
I was so lucky to have been burnt badly by loop constructs before I started learning erlang, every time i can avoid looping, a little part of me is celebrating my 9th birthday
(Having the task of processing tabular data, with contractors/people who is loosing money waiting, without using SQL because SQL is believed to be overkill for the the job results in quite painful solutions...)  |
|
|
| Back to top |
|
| RichardBlank |
Posted: Tue Dec 16, 2008 7:00 pm |
|
|
|
Joined: 15 Dec 2008
Posts: 1
Location: Rohrmoser, Costa Rica
|
|
| Back to top |
|
| wuji |
Posted: Wed Aug 15, 2012 6:57 am |
|
|
|
User
Joined: 10 Aug 2012
Posts: 654
|
nurse, started taking Adderall after reading a book that told told cheap Ralph Lauren Polo told her how to lie to her doctor to get
drug."Your life becomes a squirrel, just looking for that nut, nut, [h4]Cheap Ralph Lauren Shirts[/h4] nut, looking for that Adderall," she said.Some women start on
to keep up with the demands of career and home, home, [h3]Cheap Ralph Lauren Shirts[/h3] home, while others start looking for a quick weight loss
doctors say the situation is getting out of control."This is is replica designer bags for sale is a significant problem," said Dr. Marvin Seppala, chief medical
at Hazelden, an addiction treatment facility. "We've got an increase increase [h2]replica designer bags for sale[/h2] increase in women using drugs like Adderall ending up in |
|
|
| 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 |
|
|
|