F# 是否从后到前迭代元组?

发布于 2024-09-30 18:41:26 字数 311 浏览 1 评论 0原文

我创建了一些语言元组,当在交互式窗口中使用它时,它们会反向列出。这是正常的 F# 行为吗?

let languages = ("English", "Spanish", "Italian")
let x, y, z = languages

val languages : string * string * string = ("English", "Spanish", "Italian")
val z : string = "Italian"
val y : string = "Spanish"
val x : string = "English"

I've created a little tuple of langauges and when using it in the interactive window they are listed in reverse. Is this normal F# bahavior?

let languages = ("English", "Spanish", "Italian")
let x, y, z = languages

val languages : string * string * string = ("English", "Spanish", "Italian")
val z : string = "Italian"
val y : string = "Spanish"
val x : string = "English"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海未深 2024-10-07 18:41:26

您将同时创建三个具有独立值的变量。顺序与此处无关。 F# Interactive 可以按任意顺序打印值。

重要的是代码中的顺序评估,以及 spec 表示当您调用函数或构造函数、创建记录等时,它是从左到右的。

> (printfn "a", printfn "b");;
a
b

You're creating three variables, at the same time, with independant values. Order is not relevant here. F# interactive could print the values in any order.

What is important is order evaluation in your code, and the spec says it's from left to right when you're calling a function or constructor, creating a record, and so on.

> (printfn "a", printfn "b");;
a
b
我还不会笑 2024-10-07 18:41:26

这也是当我在机器上分解元组时 FSI 打印元组的方式。

例如:

设 x, y = ("a", "b");;

val y : string = "b"
val x : string = "a"

它以“反向”方式打印有点奇怪,但我不确定是否会称其为 F# 行为,也称其为 FSI 行为或漂亮的打印行为。

如果您需要所有详细信息,可以随时查看源代码:

http://fsharppowerpack.codeplex。 com/

That is also how FSI prints tuples when I decompose them them on my machine.

eg:

let x, y = ("a", "b");;

val y : string = "b"
val x : string = "a"

It's a little weird that it prints in "reverse", but I'm not sure that I would call it F# behavior as much as it is FSI behavior or pretty print behavior.

If you're want all the details, you can always have a look at the source code:

http://fsharppowerpack.codeplex.com/

月下凄凉 2024-10-07 18:41:26

我不确定是否存在联系,但是将 F# 表达式包装在引用中可以让您深入了解该语言的语义,并且您可以在下面看到命名值确实以相反的顺序绑定,如 FSI 中所示:

> <@ let languages = ("English", "Spanish", "Italian") in let x, y, z = languages in () @> |> string;;
val it : string =
  "Let (languages,
     NewTuple (Value ("English"), Value ("Spanish"), Value ("Italian")),
     Let (z, TupleGet (languages, 2),
          Let (y, TupleGet (languages, 1),
               Let (x, TupleGet (languages, 0), Value (<null>)))))"

请注意,这仍然与 @Laurent 的答案一致,该答案断言元组构造中的参数表达式是从左到右计算的。在以下示例中,了解如何将元组构造的结果绑定到中间命名值,然后使用无副作用的 TupleGet 表达式对该中间命名值进行解构。

> <@ let x,y = (stdin.Read(), stdin.ReadLine()) in () @> |> string;;
val it : string =
  "Let (patternInput,
     NewTuple (Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
                                 [])), Int32 Read(), []),
               Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
                                 [])), System.String ReadLine(), [])),
     Let (y, TupleGet (patternInput, 1),
          Let (x, TupleGet (patternInput, 0), Value (<null>))))"

I'm not sure if there is a connection, but wrapping F# expressions in Quotations can give you insight into the semantics of the language, and you can see in the following that the named values are indeed bound in reverse order like shown in FSI:

> <@ let languages = ("English", "Spanish", "Italian") in let x, y, z = languages in () @> |> string;;
val it : string =
  "Let (languages,
     NewTuple (Value ("English"), Value ("Spanish"), Value ("Italian")),
     Let (z, TupleGet (languages, 2),
          Let (y, TupleGet (languages, 1),
               Let (x, TupleGet (languages, 0), Value (<null>)))))"

Note that this is still consistent with @Laurent's answer, which asserts that argument expressions in tuple construction are evaluated from left to right. In the following example, see how the result of the tuple construction is bound to an intermediate named value, which is then deconstructed using side-effects free TupleGet expressions.

> <@ let x,y = (stdin.Read(), stdin.ReadLine()) in () @> |> string;;
val it : string =
  "Let (patternInput,
     NewTuple (Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
                                 [])), Int32 Read(), []),
               Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
                                 [])), System.String ReadLine(), [])),
     Let (y, TupleGet (patternInput, 1),
          Let (x, TupleGet (patternInput, 0), Value (<null>))))"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文