Not knowing how new (or not) to Erlang the OP might be, I just wanted to make sure that the OP is aware of the existence of lists:seq/2, even if it's stating the obvious.<br><br><div class="gmail_quote">On Wed, Jul 2, 2008 at 5:03 AM, Lev Walkin <<a href="mailto:vlm@lionet.info">vlm@lionet.info</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">not norwegian swede wrote:<br>
> can i use conses in erlang?<br>
> like in scheme, then i only need one function. is the range func in<br>
> erlang ood erlang-style or is there a better way to do it?<br>
><br>
> (define (seq a b)<br>
> (if (< a b)<br>
> (cons a (seq (+ a 1) b))<br>
> '()))<br>
><br>
> -module(test).<br>
> -export([range/2]).<br>
><br>
> range(Start, End) when Start < End, is_integer(Start), is_integer(End) -><br>
> seq(Start, End, []).<br>
><br>
> seq(Start, End, List) -><br>
> if Start =< End -><br>
> seq(Start + 1, End, List ++ [Start]);<br>
> true -><br>
> List<br>
> end.<br>
<br>
<br>
</div>seq(Start, End) when is_integer(Start), is_intger(End) -><br>
seq(End, Start, []).<br>
<br>
seq(End, Start, Acc) when Start >= End -><br>
seq(End - 1, Start, [End|Acc]);<br>
seq(_End, _Start, Acc) -> Acc.<br>
<br>
<br>
this approach also has a linear complexity, instead of being O(N^2)<br>
in your cas.<br>
<font color="#888888"><br>
--<br>
vlm<br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought.<br>
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)