<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in '2.6 One time assigments' | Pragmatic Forums</title>
    <link>http://fora.pragprog.com/forums/27/topics/275</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>2.6 One time assigments posted by Alain O'Dea @ Wed, 27 Feb 2008 00:56:13 -0000</title>
      <description>&lt;p&gt;Iterative or repeating algorithms are fairly easy to port from an imperative language (like Java) to Erlang.&lt;/p&gt;


	&lt;p&gt;For example, you want to gather some interesting property from a set of items.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;In Java you would use a for loop and an accessor method:&lt;/strong&gt;&lt;br /&gt;&lt;pre&gt;
void process(List list) {
    List output = new ArrayList();
    for (Item item : list) {
        output.add(item.getInterestingProperty());
    }
    return output;
}
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;In Erlang you would fold the list and extract the property with pattern matching:&lt;/strong&gt;&lt;br /&gt;&lt;pre&gt;
process(List) -&amp;gt;
    lists:foldl(fun process/2, [], List).

process(#item{interestingProperty=InterestingProperty}, Output) -&amp;gt;
    [InterestingProperty|Output].
&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 27 Feb 2008 00:56:13 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:275:2336</guid>
      <author>Alain O'Dea</author>
      <link>http://fora.pragprog.com/forums/27/topics/275</link>
    </item>
    <item>
      <title>2.6 One time assigments posted by Joe Armstrong @ Tue, 26 Feb 2008 10:04:34 -0000</title>
      <description>&lt;p&gt;Good question !&lt;/p&gt;


	&lt;p&gt;Erlang has no loops or for/next cycles so the problem does not occur.&lt;/p&gt;


	&lt;p&gt;Basically you cannot say X = X + 1, you have to invent a new variable X1 and make sure that X1 goes &amp;#8220;out of scope&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;So the old code (in an imperative language)&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;foo(X) {
    X = X + 1,
    Y = bar(X),
    ho(X, Y),
    joe(Y).
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;becomes&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;foo(X) -&amp;gt; 
    X1 = X + 1,
    Y = bar(X1),
    ho(X1, Y),
    %% At this point X1 is no longer referred to so the space for X1 can  be freed
    joe(Y).&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;We now call joe(Y) but where does joe(Y) return to? &amp;#8211; to the end of foo(X) where it finds a return instruction&lt;br /&gt;ultimately joe(Y) returns to the place that called foo(X) &amp;#8211; the point is that Y is never refereed to again&lt;br /&gt;in the body of foo(X) and can be garbage collected.&lt;/p&gt;


	&lt;p&gt;So we create new variables X1, X2, X3, .... and the space for these can be reclaimed &lt;br /&gt;when the program can no longer access these variables.&lt;/p&gt;


	&lt;p&gt;To get the effect of a loop we write a tail-recursive recursive function. So to sum the integers from 1 to N&lt;br /&gt;we might write.&lt;/p&gt;


	&lt;p&gt;sum(N) -&amp;gt; sum(N, 0).&lt;/p&gt;


	&lt;p&gt;sum(0, Sum) -&amp;gt; Sum;&lt;br /&gt;sum(N, Sum) -&amp;gt;
     Sum1 = Sum + 1,    %% create a new value of Sum
     N1 = N &amp;#8211; 1,        %% and a new value of N
     sum(N1, Sum1).     %% call sum (again)&lt;/p&gt;


	&lt;p&gt;The last line (which calls sum) merely jumps to the start of the sum routine. Although this is a call&lt;br /&gt;the system does not return to site of the call, since this merely returns immediately, instead it returns,&lt;br /&gt;(this is called &amp;#8220;last-call&amp;#8221; optimization). Having jumped to the start of sum the variables&lt;br /&gt;N1 and Sum1 are out of scope and can never be accessed again, so they get garbaged. The Erlang compiler&lt;br /&gt;recognises things like this and attempts to save space by storing N1 in the register used for N   (if this&lt;br /&gt;is possible).&lt;/p&gt;


	&lt;p&gt;This above code &lt;strong&gt;is&lt;/strong&gt; a loop (after compilation)&lt;/p&gt;


	&lt;p&gt;&lt;span class="caps"&gt;BTW&lt;/span&gt; We&amp;#8217;d really write the second clause of sum as:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;sum(N, Sum) -&amp;gt; sum(N-1, Sum+N)&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Without the temporary variables. The version I wrote was so you can see what happens to the variables.&lt;/p&gt;


	&lt;p&gt;Hope that helps!&lt;/p&gt;


	&lt;p&gt;/Joe&lt;/p&gt;</description>
      <pubDate>Tue, 26 Feb 2008 10:04:34 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:275:2330</guid>
      <author>Joe Armstrong</author>
      <link>http://fora.pragprog.com/forums/27/topics/275</link>
    </item>
    <item>
      <title>2.6 One time assigments posted by Alexey Kuznetsov @ Tue, 26 Feb 2008 08:14:52 -0000</title>
      <description>&lt;p&gt;I agree. And i see that style is a good style. But really i try to imagine situation that can totally brake that logic &amp;#8211; loops. If you try use loops in any languages you must reuse loop iterator all time. And that is a question &amp;#8211; how to solve that. There Joe explain new style, but don&amp;#8217;t explain main differents between languages.&lt;/p&gt;</description>
      <pubDate>Tue, 26 Feb 2008 08:14:52 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:275:2329</guid>
      <author>Alexey Kuznetsov</author>
      <link>http://fora.pragprog.com/forums/27/topics/275</link>
    </item>
    <item>
      <title>2.6 One time assigments posted by Alain O'Dea @ Tue, 26 Feb 2008 00:54:41 -0000</title>
      <description>&lt;p&gt;I think Joe was trying to get at a different kind of resetting of a variable. Think of Java.&lt;/p&gt;


&lt;pre&gt;String street = "1 Infinite Loop";
street = street.replace("1", "1A");
&lt;/pre&gt;

	&lt;p&gt;Erlang&amp;#8217;s variables are a lot like Java finals:&lt;br /&gt;&lt;pre&gt;final String street = "1 Infinite Loop";
final String correctedStreet = street.replace("1", "1A");
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;In Erlang:&lt;br /&gt;&lt;pre&gt;Street = "1 Infinite Loop",
{ok, CorrectedStreet, _} = regexp:sub(Street, "1", "1A").
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;The &amp;#8221;=&amp;#8221; operator in Erlang is not assignment although it may appear to be. It declares a constraint. It declares that the left hand and right hand sides are equal. By necessity everything on the right-hand side must be bound or set while the left-hand side may have unbound variables which will become bound to satisfy the constraint as declared.&lt;/p&gt;</description>
      <pubDate>Tue, 26 Feb 2008 00:54:41 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:275:2328</guid>
      <author>Alain O'Dea</author>
      <link>http://fora.pragprog.com/forums/27/topics/275</link>
    </item>
    <item>
      <title>2.6 One time assigments posted by Alexey Kuznetsov @ Mon, 25 Feb 2008 15:21:08 -0000</title>
      <description>&lt;p&gt;At end of 2.6 you can read bullet:&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;At this point you might be wondering how it&#8217;s possible to program with- &lt;br /&gt;&amp;gt; out variables. How can you express something like X = X + 1 in Erlang? &lt;br /&gt;&amp;gt; The answer is easy. Invent a new variable whose name hasn&#8217;t been used &lt;br /&gt;&amp;gt; before (say X1), and write X1 = X + 1.&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;But my question is how it work for in for/next cycles. And &lt;span class="caps"&gt;IMHO&lt;/span&gt; right explanation is: recursion examples.&lt;/p&gt;</description>
      <pubDate>Mon, 25 Feb 2008 15:21:08 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:275:2325</guid>
      <author>Alexey Kuznetsov</author>
      <link>http://fora.pragprog.com/forums/27/topics/275</link>
    </item>
  </channel>
</rss>
