[erlang-questions] conses in erlang?

Edwin Fine <>
Wed Jul 2 16:25:20 CEST 2008


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 <> wrote:

> not norwegian swede wrote:
> >   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(Start, End) when is_integer(Start), is_intger(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
> 
> 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080702/18667053/attachment.html>


More information about the erlang-questions mailing list