gpt4 book ai didi

erlang - Core Erlang 的 `letrec` 有什么用?

转载 作者:行者123 更新时间:2023-12-04 01:07:39 25 4
gpt4 key购买 nike

Core Erlang 的 letrec 有什么用?

Richard Carlsson 在 "Introduction to Core Erlang" 中写道:

Furthermore letrec expressions allow local (recursive) function definitions, which ERLANG itself does not have, but which are often useful in transformations.

letrec 对哪些转换有用?

从 Erlang 翻译时,erlc 实际上会生成 letrec 吗?或者只有在从非 Erlang 源语言翻译时才会生成 letrec

最佳答案

Nowadays, list comprehensions are translated to letrecs(which are available in Core Erlang), and the letrecs are inturn translated to ordinary recursive functions.

请注意,“现在”是 2010 年。另外,在 EEP 52: Allow key and size expressions in map and binary matching 中有一些使用它的示例:

In OTP 23, all variables used in a segment size expression must be alreadybound in the enclosing environment. The previous example must be rewrittenlike this using nested cases:

   'foo'/1 =
fun (_0) ->
case _0 of
<#{#<Sz>(16,1,'integer',['unsigned'|['big']]),
#<_2>('all',1,'binary',['unsigned'|['big']])}#> when 'true' ->
case _2 of
<#{#<X>(Sz,1,'integer',['unsigned'|['big']])}#> when 'true' ->
X
<_3> when 'true' ->
%% Raise function_clause exception.
.
.
.
end
<_4> when 'true' ->
%% Raise function_clause exception.
.
.
.
end

However, as can be seen from the example, the code for raising the function_clauseexception has been duplicated. The code duplication is no big deal in this simpleexample, but it would be in a function where the binary matching clause was followedby many other clauses. To avoid the code duplication, we must use letrec withthe letrec_goto annotation:

   'foo'/1 =
fun (_0) ->
( letrec
'label^0'/0 =
fun () ->
case _0 of
<_1> when 'true' ->
%% Raise function_clause exception.
.
.
.
end
in case _0 of
<#{#<Sz>(16,1,'integer',['unsigned'|['big']]),
#<_2>('all',1,'binary',['unsigned'|['big']])}#> when 'true' ->
case _2 of
<#{#<X>(Sz,1,'integer',['unsigned'|['big']])}#> when 'true' ->
X
<_3> when 'true' ->
apply 'label^0'/0()
end
<_4> when 'true' ->
apply 'label^0'/0()
end
-| ['letrec_goto'] )

关于erlang - Core Erlang 的 `letrec` 有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65905246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com