[erlang-questions] Thinking in Erlang

黃耀賢 (Yau-Hsien Huang) <>
Fri Apr 30 09:07:23 CEST 2010


2010/4/30 黃耀賢 (Yau-Hsien Huang) <>

> q(N, Q) ->
>     receive
> {Pid, break} ->
>     Pid ! Q;
> {Pid, E} ->
>     Q1 = [E|Q],
>     if length(Q1) > N ->
>     Pid ! {lists:sublist(Q1, 1, N),
>    lists:sublist(Q1, N+1, N+1)},
>     q(N, lists:sublist(Q1, 1, N));
>        true ->
>     Pid ! {Q1, []},
>     q(N, Q1)
>     end
>     end.
>

A question. In q/2, it takes a list of length N and makes another one of
length N+1.
Then it sinks into a recursive call with at most N elements of the list Q1.
Does it use redundant data for recursion? Or there's some smart way in the
abstract machine to trim redundancy?


More information about the erlang-questions mailing list