最好使用“and”或“在”中当链接“let”时声明?
我意识到这可能是一个愚蠢的问题,但是...
如果我链接一堆 let
语句,这些语句不需要知道彼此的值,是不是更好使用and
还是in
?
例如,如果有的话,其中哪一个更可取:
let a = "foo"
and b = "bar"
and c = "baz"
in
(* etc. *)
或者
let a = "foo" in
let b = "bar" in
let c = "baz"
in
(* etc. *)
我的直觉告诉我,前者应该“更好”(通过“更好”的非常狭隘的定义),因为它创建了必要的最小数量的范围,而后者是编译器/解释器会注意的范围内的范围,但最终并不重要且不必要地深入。
I realize this is probably a silly question, but...
If I'm chaining a bunch of let
statements which do not need to know each other's values, is it better to use and
or in
?
For example, which of these is preferable, if any:
let a = "foo"
and b = "bar"
and c = "baz"
in
(* etc. *)
or
let a = "foo" in
let b = "bar" in
let c = "baz"
in
(* etc. *)
My intuition tells me the former ought to be "better" (by a very petty definition of "better") because it creates the minimum number of scopes necessary, whereas the latter is a scope-within-a-scope-within-a-scope which the compiler/interpreter takes care to note but is ultimately unimportant and needlessly deep.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的意见是
in
更好。使用和
意味着这些定义是相互依赖的。我认为最好澄清事实并非如此。另一方面,一些 OCaml 程序员确实更喜欢和
来定义非常短的定义,其中稍微更紧凑的符号可以显得更清晰。当您可以将定义放在一行中时尤其如此:My opinion is that
in
is better. The use ofand
implies that the definitions are mutually dependent on each other. I think it is better to be clear that this is not the case. On the other hand, some OCaml programmers do preferand
for very short definitions, where the slightly more compact notation can appear cleaner. This is especially true when you can fit the definitions on a single line:“哪个更好?”这个问题的答案只有当解释没有不同时才真正有意义。 ML 系列语言(至少 SML 和 OCaml)同时具有并行初始化形式(
and
)和顺序的、本质上嵌套范围的形式(in
),因为它们是在某些情况下两者都有用。在您的情况下,语义是相同的,因此您需要回答“什么最适合您?”的问题。也许(如果这还为时过早的话)“哪个可能会更有效地执行?”在您的情况下,替代方案是:
对于
and
情况:评估一堆字符串并对三个标识符进行并行绑定。对于
in
情况:将foo
绑定到a
,然后绑定bar
到b
,然后将baz
绑定到c
。哪个读起来更好?在这种情况下,这是一个折腾,因为结果并不重要。你可以对一些说英语的人进行民意调查,但我怀疑会有很多人偏爱。也许大多数人会喜欢
and
,因为它分隔绑定,在表达式之前留下唯一的in
。至于什么执行效率更高,我认为一个好的编译器可能会产生相同的代码,因为它可以检测绑定的顺序并不重要。但也许您有一个编译器,可以为多核处理器生成代码,并且可以更好地使用
和
。或者可能是一个简单的编译器,它将所有 RHS 写入临时存储,然后绑定它们,从而使和
情况变慢。这些可能是非必要的优化问题,特别是因为它们涉及绑定并且可能不在紧密循环中。
底线:在这种情况下,这是一个真正的折腾;只要确保在结果确实重要时正确使用并行与顺序即可。
The answer to the question "which is better?" only really makes sense when the interpretations do not differ. The ML family of languages (at least SML and OCaml) have both a parallel initialization form (
and
) and a sequential, essentially nested-scope form (in
) because they are both useful in certain situations.In your case the semantics are the same, so you are left to answer the question "what reads best to you?" and maybe (if this is not premature) "which might be executed more efficiently?" In your case the alternatives are:
For the
and
case: evaluate a bunch of strings and do a parallel binding to three identifiers.For the
in
case: bindfoo
toa
, then bindbar
tob
, then bindbaz
toc
.Which reads better? It is a toss up in this case because the outcome does not matter. You can poll some English speakers but I doubt there will be much preference. Perhaps a majority will like
and
as it separates bindings leaving the solein
before the expression.As to what executes more efficiently, I think a good compiler will likely produce the same code because it can detect the order of binding will not matter. But perhaps you have a compiler that generates code for a multicore processor that does better with
and
. Or maybe a naive compiler which writes all the RHSs into temporary storage and then binds them, making theand
case slower.These are likely to be non-essential optimization concerns, especially since they involve bindings and are probably not in tight loops.
Bottom line: it's a true toss-up in this case; just make sure to use parallel vs. sequencial correctly when the outcome does matter.
我想说 in 更好,因为它缩小了范围并更好地表达了意图。如果我看到所有这些定义以共享范围的方式链接在一起,我会觉得这样做是有原因的,并且会寻找它们如何相互影响。
I would say in is better because it reduces scope and better expresses intent. If I see all these defintions chained together in a shared scope manner I would be under the impression that it was done for a reason and would be looking for how they effect each other.
我相信这与 Lisp 中
let
和let*
之间的区别相同。let*
(功能类似于in..in..
结构 - 问题中的第二个结构)有时用于隐藏命令式编程,因为它保证了表达式的顺序执行(参见 Paul Graham 在 On Lisp< 中关于 let* 的说法< /a>)。所以,我想说前一种结构更好。但事实是,我认为后者在我见过的 Ocaml 程序中更常见。可能只是更容易使用,让人们可以在以前命名的表达式上进行构建。
It's the same as the difference between
let
andlet*
in Lisp, I believe.let*
(similar in functionality to thein..in..
structure - the second structure in your question) is sometimes used to hide imperative programming since it guarantees sequential execution of expressions (see what Paul Graham had to say about let* in On Lisp).So, I'd say the former construct is better. But the truth is, I think the latter is more common in the Ocaml programs I have seen. Probably just easier to use in letting one build on previously named expressions.