[erlang-questions] erlang / laziness
Jesper Louis Andersen
jesper.louis.andersen@REDACTED
Sun Mar 2 15:15:26 CET 2014
On Sun, Mar 2, 2014 at 12:32 PM, t x <txrev319@REDACTED> wrote:
> Hi,
>
> I'm from a clojure background, where many things are lazy by
> default, including fundamental constructs, like map.
>
> (def x (map func lst))
> ;; func is not called on lst yet
>
>
In erlang, this would be:
x() ->
[Func(L) || L <- list()].
The evaluation will happen when x/0 is invoked, but in Erlang the
evaluation order is strict by default and nothing is evaluated "on demand"
unless you explicitly ask for an iterator-like construct. Clojure also uses
a strict evaluation order, but many data structures are consumed by
iteration in Clojure and this means you have a way to terminate a
computation in the middle of a map by not demanding the full sequence.
> some-expression,
> [ func(X) || X <- Lst],
> some-other-expression
>
> Is this assumption correct?
>
In the above, you essentially have three Expressions
E1,
E2,
E3.
The rule in Erlang is that they are evaluated in order, i.e., E1 before E2
before E3. And they are always evaluated for their eventual effect, even if
you don't bind the result.
--
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140302/05569f28/attachment.htm>
More information about the erlang-questions
mailing list