2 Jul 16:25
Re: conses in erlang?
From: Edwin Fine <erlang-questions_efine <at> usa.net>
Subject: Re: conses in erlang?
Newsgroups: gmane.comp.lang.erlang.general
Date: 2008-07-02 14:25:20 GMT
Subject: Re: conses in erlang?
Newsgroups: gmane.comp.lang.erlang.general
Date: 2008-07-02 14:25:20 GMT
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.
On Wed, Jul 2, 2008 at 5:03 AM, Lev Walkin <vlm <at> lionet.info> wrote:
not norwegian swede wrote:seq(Start, End) when is_integer(Start), is_intger(End) ->
> can i use conses in erlang?
> like in scheme, then i only need one function. is the range func in
> erlang ood erlang-style or is there a better way to do it?
>
> (define (seq a b)
> (if (< a b)
> (cons a (seq (+ a 1) b))
> '()))
>
> -module(test).
> -export([range/2]).
>
> range(Start, End) when Start < End, is_integer(Start), is_integer(End) ->
> seq(Start, End, []).
>
> seq(Start, End, List) ->
> if Start =< End ->
> seq(Start + 1, End, List ++ [Start]);
> true ->
> List
> end.
seq(End, Start, []).
seq(End, Start, Acc) when Start >= End ->
seq(End - 1, Start, [End|Acc]);
seq(_End, _Start, Acc) -> Acc.
this approach also has a linear complexity, instead of being O(N^2)
in your cas.
--
vlm_______________________________________________
erlang-questions mailing list
erlang-questions <at> erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions
--
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.
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
<div> <p>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></p> <div class="gmail_quote">On Wed, Jul 2, 2008 at 5:03 AM, Lev Walkin <<a href="mailto:vlm <at> lionet.info">vlm <at> lionet.info</a>> wrote:<br><blockquote class="gmail_quote"> <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><br> --<br> vlm<br><div> <div></div> <div class="Wj3C7c">_______________________________________________<br> erlang-questions mailing list<br><a href="mailto:erlang-questions <at> erlang.org">erlang-questions <at> 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) </div>
RSS Feed