# [erlang-questions] how the lists:seq/3 implemented, confused when reading the source code of lists:seq/3

Taotao.Li <>
Tue Nov 4 11:32:17 CET 2014

```----Source Code--------

-spec seq(From, To, Incr) -> Seq when
From :: integer(),
To :: integer(),
Incr :: integer(),
Seq :: [integer()].

seq(First, Last, Inc)
when is_integer(First), is_integer(Last), is_integer(Inc) ->
if
Inc > 0, First - Inc =< Last;
Inc < 0, First - Inc >= Last ->
N = (Last - First + Inc) div Inc,
seq_loop(N, Inc*(N-1)+First, Inc, []);
Inc =:= 0, First =:= Last ->
seq_loop(1, First, Inc, [])
end.

seq_loop(N, X, D, L) when N >= 4 ->
Y = X-D, Z = Y-D, W = Z-D,
seq_loop(N-4, W-D, D, [W,Z,Y,X|L]);
seq_loop(N, X, D, L) when N >= 2 ->
Y = X-D,
seq_loop(N-2, Y-D, D, [Y,X|L]);
seq_loop(1, X, _, L) ->
[X|L];
seq_loop(0, _, _, L) ->
L.

-----------------------

-----Problem-----------
when I entered lists:seq(-10, 10, 2).
what happened?
I think it should execute 'Inc > 0, First-Inc =< Last;' and in this way it should just returning nothing.

-----------------------

----- 原始邮件 -----

Send erlang-questions mailing list submissions to

To subscribe or unsubscribe via the World Wide Web, visit
http://erlang.org/mailman/listinfo/erlang-questions
or, via email, send a message with subject or body 'help' to

You can reach the person managing the list at

than "Re: Contents of erlang-questions digest..."

Today's Topics:

1. Re:  DTLS/SRTP for WebRTC (Ingela Andin)
2. Re:  spawn and controlling_process (Hynek Vychodil)
3. Re:  spawn and controlling_process (Jachym Holecek)
4. Re:  spawn and controlling_process (Gadi Srebnik)

----------------------------------------------------------------------

Message: 1
Date: Sun, 2 Nov 2014 20:03:28 +0100
From: Ingela Andin <>
To: pablo platt <>
Cc: erlang questions <>
Subject: Re: [erlang-questions] DTLS/SRTP for WebRTC
Message-ID:
<CAFj9NSTjtsh9u+LmKm-Pv4YM5OJ8Fz=>
Content-Type: text/plain; charset="utf-8"

Hi!

2014-10-30 22:33 GMT+01:00 pablo platt <>:

> Hi,
>
> What is the state of DTLS in OTP?
>

Alas Ericsson has prioritised quite a few other things higher than DTLS
just lately so the implementation has been delayed.
I hope it will get back on track soon but I am afraid you can no count on
it in a short term perspective.

Regards Ingela Erlang/OTP team - Ericsson AB

>
> I'm interested in using DTLS/SRTP in Erlang for WebRTC.
>
> I've found two implementations:
>
> Clean code but I couldn't make it work because it's missing SRTP support.
>
> This works quite well. I'm able to connect clients to the server:
>
> The problem is that some clients can't connect and I'm getting HANDSHAKE
> packets while in the CIPHER state in the following order:
> HANDSHAKE, SERVER_HELLO
> HANDSHAKE, CERTIFICATE
> HANDSHAKE, SERVER_KEY_EXCHANGE
> HANDSHAKE, CERTIFICATE_REQUEST
> HANDSHAKE, SERVER_HELLO_DONE
>
> I'm assuming that is related to wrong state change or retransmission.
> The code only implement retransmission for two packet types and have the
> following comment:
> %% problem remainning: different strategy should be adopted while
> state==hello or cipher.
>
> Does anyone use DTLS/SRTP in Erlang and help solve this issue or recommend
> another solution?
>
> Thanks
>
>
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141102/0530050f/attachment-0001.html>

------------------------------

Message: 2
Date: Mon, 3 Nov 2014 10:06:28 +0100
From: Hynek Vychodil <>
Cc: erlang-questions <>
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID:
<>
Content-Type: text/plain; charset="utf-8"

No, there is not any difference. Both versions of code is compiled exactly
same:

fun1(Socket,Mod, Fun, Args) ->
Pid = erlang:spawn(Mod, Fun, Args),
gen_tcp:controlling_process(Socket,Pid).

fun2(Socket,Mod, Fun, Args) ->
gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)).

Then you can compile it with 'S' and get "assembler":

{function, fun1, 4, 2}.
{label,1}.
{line,[{location,"test2.erl",5}]}.
{func_info,{atom,test2},{atom,fun1},4}.
{label,2}.
{allocate,1,4}.
{move,{x,0},{y,0}}.
{move,{x,1},{x,0}}.
{move,{x,2},{x,1}}.
{move,{x,3},{x,2}}.
{line,[{location,"test2.erl",6}]}.
{call_ext,3,{extfunc,erlang,spawn,3}}.
{move,{x,0},{x,1}}.
{move,{y,0},{x,0}}.
{line,[{location,"test2.erl",7}]}.
{call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.

{function, fun2, 4, 4}.
{label,3}.
{line,[{location,"test2.erl",9}]}.
{func_info,{atom,test2},{atom,fun2},4}.
{label,4}.
{allocate,1,4}.
{move,{x,0},{y,0}}.
{move,{x,1},{x,0}}.
{move,{x,2},{x,1}}.
{move,{x,3},{x,2}}.
{line,[{location,"test2.erl",10}]}.
{call_ext,3,{extfunc,erlang,spawn,3}}.
{move,{x,0},{x,1}}.
{move,{y,0},{x,0}}.
{line,[{location,"test2.erl",10}]}.
{call_ext_last,2,{extfunc,gen_tcp,controlling_process,2},1}.

As you can see, both functions are exactly same. Why should be there any
difference.

Hynek Vychodil

On Sat, Nov 1, 2014 at 11:34 AM, Gadi Srebnik <> wrote:

> Hi all, my first question here.
>
> How safe it is to use controlling_process after spawn? I am using
> additional gen_tcp:controlling_process inside the Mod:Fun itself after few
> additional commands. Is it possible that process will not be owner by then?
>
>
> Pid = erlang:spawn(Mod, Fun, Args),
>
>  gen_tcp:controlling_process(Socket,Pid),
>
> Is one-liner safer?
>
> gen_tcp:controlling_process(Socket,erlang:spawn(Mod, Fun, Args)),
>
>
> Thanks,
>
>
>
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141103/260ac0e5/attachment-0001.html>

------------------------------

Message: 3
Date: Mon, 3 Nov 2014 04:30:26 -0500
From: Jachym Holecek <>
Cc:
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID: <>
Content-Type: text/plain; charset=us-ascii

> How safe it is to use controlling_process after spawn? I am using
> additional gen_tcp:controlling_process inside the Mod:Fun itself after few
> additional commands. Is it possible that process will not be owner by then?

Yes, there's a race condition there, you need explicit synchronization to
be safe:

start_worker(Sock) ->
Pid = erlang:spawn(?MODULE, worker_init, [Sock]),
gen_tcp:controlling_process(Sock, Pid),
Pid ! proceed.

worker_init(Sock) ->
proceed ->
%% Okay now we actually own it.
worker_loop(Sock)
after 1000 ->
exit(things_seem_awfully_slow_today)
end.

I wish there were a variant of controlling_process that would advise the
new owner automatically by sending {tcp_handover, Sock, Info} to it, the
last item being arbitrary term passed by previous owner. No idea how hard
would it be to implement.

BR,
-- Jachym

------------------------------

Message: 4
Date: Mon, 3 Nov 2014 11:34:31 +0200
To:
Subject: Re: [erlang-questions] spawn and controlling_process
Message-ID:
<CAFjX2eC2y8oE4O7COpiwpwhExFv-Zo0Gm+iF_fHexXRZOkh=>
Content-Type: text/plain; charset="utf-8"

Thank you, Jachym and Hynek.

On Mon, Nov 3, 2014 at 11:30 AM, Jachym Holecek <>
wrote:

> > How safe it is to use controlling_process after spawn? I am using
> > additional gen_tcp:controlling_process inside the Mod:Fun itself after
> few
> > additional commands. Is it possible that process will not be owner by
> then?
>
> Yes, there's a race condition there, you need explicit synchronization to
> be safe:
>
>   start_worker(Sock) ->
>       Pid = erlang:spawn(?MODULE, worker_init, [Sock]),
>       gen_tcp:controlling_process(Sock, Pid),
>       Pid ! proceed.
>
>   worker_init(Sock) ->
>           proceed ->
>               %% Okay now we actually own it.
>               worker_loop(Sock)
>       after 1000 ->
>           exit(things_seem_awfully_slow_today)
>       end.
>
> I wish there were a variant of controlling_process that would advise the
> new owner automatically by sending {tcp_handover, Sock, Info} to it, the
> last item being arbitrary term passed by previous owner. No idea how hard
> would it be to implement.
>
> BR,
>         -- Jachym
>

--
VP Communications
m: +972 54 5929261
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141103/72d1c16d/attachment-0001.html>

------------------------------

_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions

End of erlang-questions Digest, Vol 190, Issue 1
************************************************

--

---------------------------------------------------------------------------

Thanks & Best regards

Address :Wanxiang Towen 8 F, Lujiazui West Rd. No.99, Pudong New District, Shanghai, 200120

```