# [erlang-questions] Newbie Question, brute force method.

Jon Watte <>
Sat Sep 3 19:26:37 CEST 2011

```With tail recursion, each for loop turns into a function of its own.

stuff_a()
for (a=0; a != b; ++a) {
c += stuff_b(a)
}
stuff_c()

turns into:

stuff_a()
stuff_b_loop(a, b, 0);
stuff_c()

stuff_b_loop(A, A, R) -> R;
stuff_b_loop(A, B, R) ->
stuff_b_loop(A+1, B, R + stuff_b(A)).

This can almost always be re-written "mechanically" if you just think of the
loop as a tail-recursive function with start/stop/result arguments. You
could even declare a "for()" function that takes a fun():

for(Limit, Limit, _, Result) -> Result;
for(Start, Limit, Fun, Result) ->
for(Start+1, Limit, Fun, Fun(Result)).

This would be used in the above as:

stuff_a(),
for(a, b, fun(C) -> C + stuff_b(C) end, 0),
stuff_c()

This, in turn, is the core of the "fold" operation on sequences and
containers. (Look at lists:foldl(), dict:fold(), and friends). Thus, for the
parts of your program that just iterate over the items in an element, you
can use lists:foldl() as the loop construct.

Sincerely,

jw

--
Americans might object: there is no way we would sacrifice our living
standards for the benefit of people in the rest of the world. Nevertheless,
whether we get there willingly or not, we shall soon have lower consumption
rates, because our present rates are unsustainable.

On Fri, Sep 2, 2011 at 3:21 AM, Bangon Kali <> wrote:

> Hi guys! I'm a newbie here. I'm very new to Erlang. Although I've been
> studying well im having problems converting sequential programs in to
> Recursive ones. I'm using this book Pragmatic Programming Erlang (2007).
> I've been half way in my readings. Although I kinda get some of the pieces
> along the way, I have been having problems with some parts. Generally on
> how
> to convert very sequential programs in to recursive ones.
>
> In order to test my knowledge in Erlang I have setup a challenge to myself,
> by converting  http://j.mp/r8hS3C this program  that I've made in several
> other languages (C, Python, Javscript) into Erlang. This program basically
> just Finds the set of Realistic Resistors and Capacitor for an Astable 555
> IC implementation.
>
> The program uses the brute force technique by testing all possible values
> and calculating the frequency. It will then compare the percentage
> difference between the previous frequency to the current frequency, if the
> new frequency is lower, then it will save the R1, R2, and C combinations.
> It
> will do in a loop until all possible realistic values of R1, R2 and C are
> tested.
>
> a simple pseudo code might be easier to understand
>
>
>
> where:
>   get_error returns the percentage difference between 2 values
>   get_frequency returns the frequency of an R1, R2 and C combination
>   f_desired is the frequency target
>
> the complete source code for this program (in some languages) are available
> on this bitbucket site: http://j.mp/r8hS3C
>
> i'm not asking for a direct translation in to erlang, but if you can easily
> make one, then that would really help, since i can study it directly and
> somehow learn to implement erlang on some other problems with the same
> situation such as this.
>
> here are some of my attempts, but im at loss at how to continue this.
>
> i hope you guys can help. just some clue, if i may ask. thanks!
>
>
> --
> View this message in context:
> http://erlang.2086793.n4.nabble.com/Newbie-Question-brute-force-method-tp3785742p3785742.html
> Sent from the Erlang Questions mailing list archive at Nabble.com.
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110903/14f31fc5/attachment.htm>
```