简体   繁体   English

F#:[0;有什么区别? 1; 2; 3; 4; 5]和[0,1,2,3,4,5]?

[英]F#: What is the difference between [0; 1; 2; 3; 4; 5] and [0, 1, 2, 3, 4, 5]?

I know how to make a list: 我知道如何制作清单:

let f n = 
    let out_listA = [ 0 .. (n - 1) ]
    let out_list =
        out_listA |> List.reduce (fun state item -> state + ", " + item) 
    out_list

I am working with on-line exercises that involving printing something that looks like the following if n = 5 . 我正在进行在线练习,如果n = 5 ,则涉及打印如下所示的内容。

[0, 1, 2, 3, 4, 5]

instead of 代替

[0; 1; 2; 3; 4; 5]

From looking on the web, the comma-separated output appears to be a list converted to a string. 从Web上查看,逗号分隔的输出似乎是转换为字符串的列表。

So, my question is what does [0, 1, 2, 3, 4, 5] represent, and what is actually taking place, when converting from the list separated by semicolons to separated by commas? 所以,我的问题是[0, 1, 2, 3, 4, 5]代表什么,以及实际发生了什么,从用分号分隔的列表转换为用逗号分隔?

Epilogue: 结语:

Fyodor Soikin's comment was correct; Fyodor Soikin的评论是正确的; I was confused. 我很困惑。

It turns out, the comma-separated list was a contrivance to print out a semicolon-separated list as a comma-separated list inside array brackets. 事实证明,以逗号分隔的列表是一个设计,用于打印出以分号分隔的列表作为数组括号内的逗号分隔列表。

The following solution came from ehotinger on github: 以下解决方案来自github上的ehotinger

open System

[<EntryPoint>]

let main argv = 
    let s = Console.ReadLine() |> int
    seq { for i in 1..s do yield i }
    |> Seq.fold (fun s i ->
        if s = "" then sprintf "%i" i
        else sprintf "%s, %i" s i) ""
    |> (fun s -> sprintf "[%s]" s)
    |> Console.WriteLine

0 // return an integer exit code

I thought the list had been converted. 我以为列表已被转换。 Well it had been converted into a string containing square brackets and a comma-separated list of numbers inside the brackets. 好吧,它已被转换为包含方括号的字符串和括号内的逗号分隔数字列表。

In F#, a comma is a delimiter for a tuple. 在F#中,逗号是元组的分隔符。

So when you see something like this: 所以,当你看到这样的事情时:

match x, y with
| 1.1, 2.2 -> doSomething()
| 2.2, 3.3 -> doSomethingElse()
| _ -> defaultAction()

that is shorthand for 这是简写

match (x, y) with
| (1.1, 2.2) -> doSomething()
| (2.2, 3.3) -> doSomethingElse()
| _ -> defaultAction()

So in fact your array [0, 1, 2, 3, 4, 5] contains only one element: the tuple (0, 1, 2, 3, 4, 5) . 所以实际上你的数组[0, 1, 2, 3, 4, 5]只包含一个元素:元组(0, 1, 2, 3, 4, 5) To verify this, try doing this quick test in your REPL: 要验证这一点,请尝试在REPL中执行此快速测试:

let test = ([0, 1, 2, 3, 4, 5] = [(0, 1, 2, 3, 4, 5)])

ADDENDUM 附录

In fact, the F# Interactive window is a very good tool for answering similar questions. 实际上,F#Interactive窗口是回答类似问题的非常好的工具。 As well as the test above, you can also simply highlight the following lines and hit Ctrl+Enter: 除了上面的测试,您还可以突出显示以下行并按Ctrl + Enter:

let value1 = [0; 1; 2; 3; 4; 5]
let value2 = [0, 1, 2, 3, 4, 5]

The output is 输出是

val value1 : int list = [0; 1; 2; 3; 4; 5]
val value2 : (int * int * int * int * int * int) list = [(0, 1, 2, 3, 4, 5)]

which answers the question very succinctly. 它非常简洁地回答了这个问题。 True to the nature of F#, it reproduces my long-winded answer in two lines. 忠实于F#的本质,它以两行重现了我冗长的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM