F# F的主参数[]#
下面是我按照Chris Smith的《编程F》一书尝试的F中的一些代码:F# F的主参数[]#,f#,F#,下面是我按照Chris Smith的《编程F》一书尝试的F中的一些代码: (* 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
(*
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
(*
超级你好世界:
获取两个命令行参数,然后打印
将它们与当前时间一起发送到控制台。
*)
开放系统
[]
let main(参数:字符串[])=
如果参数长度为2,则
failwith“错误:预期参数和”
让我们打招呼,thing=args[0],args[1]
让timeOfDay=DateTime.Now.ToString(“hh:mmtt”)
打印fn“%s,%s在%s”的日期时间
//程序退出代码
0
main([“asd”,“fgf”])|>忽略
main中有一个错误,它说:这个表达式应该有“String[]”类型,但这里是“一个列表”。但是String[]是一个字符串数组。所以我不理解我的错误。
String[]
确实是一个字符串数组,但是[“asd”,“fgf”]
不是-它是一个列表,这就是为什么会出现这个错误的原因
要创建数组,请使用[|“asd”;“fgf”|]
(请注意,在列表和数组中,;
用作分隔符-,
创建元组)
此外,不能在标记为EntryPoint
的函数后添加代码。即使可以,调用该函数也没有意义,因为它将使用命令行参数自动调用-这是EntryPoint
属性的要点。