下面是一些代码在F#中,我试过按照编程F#byChris Smith的书:
(*
Mega Hello World:
Take two command line parameters and then print
them along with the current time to the console.
*)
open System
[<EntryPoint>]
let main (args : string[]) =
if args.Length <> 2 then
failwith "Error: Expected arguments <greeting> and <thing>"
let greeting, thing = args.[0], args.[1]
let timeOfDay = DateTime.Now.ToString("hh:mm tt")
printfn "%s, %s at %s" greeting thing timeOfDay
// Program exit code
0
main(["asd","fgf"]) |> ignore
Run Code Online (Sandbox Code Playgroud)
在main中有一个错误说:这个表达式应该有'String []'类型,但是这里输入"list".但是string []是一个字符串数组.所以我不明白我的错误.
string[]确实是一个字符串数组,但["asd", "fgf"]不是 - 它是一个列表,这就是你得到那个错误的原因.
要创建一个数组,请使用[|"asd"; "fgf"|](请注意,列表和数组中的两个都;用作分隔符 - ,创建元组).
此外,在标记为的函数后,您不能拥有代码EntryPoint.即使你可以,调用该函数也没有意义,因为它已经被命令行参数自动调用 - 这就是EntryPoint属性的要点.