<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Understanding mylists:map/2' | Pragmatic Forums</title>
    <link>http://fora.pragprog.com/forums/27/topics/228</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Understanding mylists:map/2 posted by Jared Haworth @ Mon, 21 Jan 2008 00:58:34 -0000</title>
      <description>&lt;p&gt;I guess it&amp;#8217;s a side effect of coming from Ruby, where I&amp;#8217;m accustomed to having to flatten and compact my arrays all the time; putting &lt;code&gt;[1|[2,3,4,5]]&lt;/code&gt; into the Eshell gave me back &lt;code&gt;[1,2,3,4,5]&lt;/code&gt;, in harmony with your examples above.&lt;/p&gt;


	&lt;p&gt;I think the fact that the functions above return all their output in a list is probably what threw me off track, it looked like the return would be coming back as a series of nested lists.&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jan 2008 00:58:34 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:228:2198</guid>
      <author>Jared Haworth</author>
      <link>http://fora.pragprog.com/forums/27/topics/228</link>
    </item>
    <item>
      <title>Understanding mylists:map/2 posted by Alain O'Dea @ Mon, 21 Jan 2008 00:13:42 -0000</title>
      <description>&lt;p&gt;The expression [Head|Tail] in Erlang takes head as an item and Tail as a list. For example [1|[2,3,4,5]] results in [1,2,3,4,5]. In the other direction, [Head|Tail]=[1,2,3,4,5] results in Head=1 and Tail=[2,3,4,5].&lt;/p&gt;


	&lt;p&gt;The following is my impression of how Erlang would execute your example:&lt;br /&gt;&lt;pre&gt;First recursion:
map(F, [H=1|T=[2,3,4,5]]) -&amp;gt; [2*1|map(F, [2,3,4,5])
Second recursion:
map(F, [H=2|T=[3,4,5]]) -&amp;gt; [2*2|map(F, [3,4,5])
Third recursion:
map(F, [H=3|T=[4,5]]) -&amp;gt; [2*3|map(F, [4,5])
Fourth recursion:
map(F, [H=4|T=[5]]) -&amp;gt; [2*4|map(F, [5])
Fifth recursion:
map(F, [H=4|T=[]]) -&amp;gt; [2*5|map(F, [])
Sixth recursion:
map(_, []) -&amp;gt; []) -&amp;gt; []
Construct fifth:
[2*5|[]] -&amp;gt; [10]
Construct fourth:
[2*4|[10]] -&amp;gt; [8,10]
Construct third:
[2*3|[8,10]] -&amp;gt; [6,8,10]
Construct second:
[2*2|[6,8,10]] -&amp;gt; [4,6,8,10]
Construct result:
[2*1|[4,6,8,10]] -&amp;gt; [2,4,6,8,10]&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 21 Jan 2008 00:13:42 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:228:2197</guid>
      <author>Alain O'Dea</author>
      <link>http://fora.pragprog.com/forums/27/topics/228</link>
    </item>
    <item>
      <title>Understanding mylists:map/2 posted by Jared Haworth @ Sat, 19 Jan 2008 23:46:29 -0000</title>
      <description>&lt;p&gt;Hi everyone,&lt;/p&gt;


	&lt;p&gt;So, I&amp;#8217;ve finally found some time to get back into the Erlang book, and I was looking over some code as a bit of a refresher.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m kind of confused by the output of the mylists:map/2 function (and by extension, the output of the standard lists:map/2).&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
map(_, [])     -&amp;gt; [];
map(F, [H|T])  -&amp;gt; [F(H)|map(F, T)].
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The output of the second clause builds a new list with H being the result of F(H) and the tail is a recursive call to map(F, T).&lt;/p&gt;


	&lt;p&gt;When I try to map this out on paper, using input such as &lt;code&gt;L = [1, 2, 3, 4, 5]&lt;/code&gt;, and &lt;code&gt;fun(X) -&amp;gt; 2*X end&lt;/code&gt; as the fun being passed in as the first parameter, why isn&amp;#8217;t the output &lt;code&gt;[2, [4, [6, [8, [10]]]]]&lt;/code&gt; ?&lt;/p&gt;


	&lt;p&gt;Is it something particular to the internals of Erlang, or my own misunderstanding of recursive functions?&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</description>
      <pubDate>Sat, 19 Jan 2008 23:46:29 -0000</pubDate>
      <guid isPermaLink="false">fora.pragprog.com:27:228:2193</guid>
      <author>Jared Haworth</author>
      <link>http://fora.pragprog.com/forums/27/topics/228</link>
    </item>
  </channel>
</rss>
