<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 27, 2015 at 1:42 PM, harsha sri <span dir="ltr"><<a href="mailto:harsha.pic@gmail.com" target="_blank">harsha.pic@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div>Is it possible to add dynamically add elements to List in recursion?<br></div></div></div></blockquote><div><br></div><div>There is some indication, you may want to spend time with a good book on Erlang. For instance <a href="http://learnyousomeerlang.com">learnyousomeerlang.com</a> or perhaps Joe Armstrong's book on Erlang. I'm not entirely sure what you want to do, but I can explain to you what happens in the system for the lines below.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div></div>ex: List = [1,2,3]<br><br></div></div></blockquote><div><br></div><div>This will create a list [1,2,3] and bind that list to the name List.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div>lists:foreach...<br><br></div></blockquote><div><br></div><div>calling, for instance, lists:foreach(fun(Item) -> io:format("~B~n", [Item]) end, List) will not be able to change List. 'foreach/2' is executed for its side-effect only, in this case printing elements. You can use a map/2: List2 = lists:map(fun(Item) -> Item + 3 end, List), but this will</div><div><br></div><div>* Add three to each element of the list and bind the result to List2, i.e., List2 = [4,5,6]</div><div>* Not be able to add or remove elements from L</div><div>* Not change L in any way. The language uses persistent data structures, not ephemeral data structures.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>.....<br></div>List = [4 | List]<br></blockquote><div><br></div><div>The expression [4 | List] evaluates to [4,1,2,3] because it prepends (cons'es) 4 in front of the list. If you write</div><div><br></div><div>List = [4 | List]</div><div><br></div><div>then the Erlang semantics are:</div><div><br></div><div>* build up the list [4 | List] --> [4,1,2,3]</div><div>* Since 'List' is already bound, assert that the current contents of the list is [4,1,2,3]. But List is already bound to [1,2,3], so the assertion is failing.</div><div>* Crash the program with a badmatch error since the assertion is a failure</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div><div>Finally i am expecting in List: [1,2,3,4]<br></div></div></div></div></blockquote></div><div class="gmail_extra"><br></div>One way to solve this is to append two lists: List ++ [4]. This will build the single-element list [4] and then append it as a new suffix. If you want to give it a name, it has to be one that hasn't been used already in the scope, e.g.,</div><div class="gmail_extra"><br></div><div class="gmail_extra">NewList = List ++ [4].</div><div class="gmail_extra"><br></div><div class="gmail_extra">For small lists, this is a viable strategy. For large lists, append can end up being costly in execution speed, but I'd focus on solving problems first and then worrying about performance later. Especially when you are trying to learn what is going on in the first place.</div><div class="gmail_extra"><br><div>-- <br></div><div class="gmail_signature">J.</div>
</div></div>