%A 在 F# 中是什么意思?
What does %A mean in F#?
在 Microsoft Visual Studio 2015 的 F# 教程中有此代码,但略有不同。
module Integers =
/// A list of the numbers from 0 to 99
let sampleNumbers = [ 0 .. 99 ]
/// A list of all tuples containing all the numbers from 0 to 99 andtheir squares
let sampleTableOfSquares = [ for i in 0 .. 99 -> (i, i*i) ]
// The next line prints a list that includes tuples, using %A for generic printing
printfn "The table of squares from 0 to 99 is:\n%A" sampleTableOfSquares
System.Console.ReadKey() |> ignore
此代码 returns 标题 The table of squares from 0 to 99 is:
。
然后它发送 1-99 的数字及其方块。我不明白为什么需要 \n%A
,特别是为什么它必须是 A.
以下是一些其他类似的例子,但字母不同:
- 使用
%d
module BasicFunctions =
// Use 'let' to define a function that accepts an integer argument and returns an integer.
let func1 x = x*x + 3
// Parenthesis are optional for function arguments
let func1a (x) = x*x + 3
/// Apply the function, naming the function return result using 'let'.
/// The variable type is inferred from the function return type.
let result1 = func1 4573
printfn "The result of squaring the integer 4573 and adding 3 is %d" result1
- 使用
%s
module StringManipulation =
let string1 = "Hello"
let string2 = "world"
/// Use @ to create a verbatim string literal
let string3 = @"c:\Program Files\"
/// Using a triple-quote string literal
let string4 = """He said "hello world" after you did"""
let helloWorld = string1 + " " + string2 // concatenate the two strings with a space in between
printfn "%s" helloWorld
/// A string formed by taking the first 7 characters of one of the result strings
let substring = helloWorld.[0..6]
printfn "%s" substring
这让我有点困惑,因为他们必须要有他们的信件,否则他们将无法工作,有人也可以。请解释 %a
、%d
和 %s
以及任何其他(如果有的话)以及 \n
的含义。
本页对此进行了全部解释:https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting
F# 使用类似于 C 的合理标准格式字符串。
总结一下:
%s prints a string
%d is an integer
唯一奇怪的是%A
,它会尝试打印任何东西
\n
只是一个换行符
符号 %A、%s 和 %d 是占位符。当通过其参数值调用函数 printfn 时,它们将被替换。每个占位符都需要一个特定的参数类型:
- %s - 字符串
- %d - 有符号整数
- %A - pretty-printing 元组、记录和联合类型
以下是一些可以在 F# REPL 中执行的示例:
> printfn "a string: %s" "abc";;
a string: abc
val it : unit = ()
> printfn "a signed int: %d" -2;;
a signed int: -2
val it : unit = ()
> printfn "a list: %A" [1;2;3];;
a list: [1; 2; 3]
val it : unit = ()
> printfn "a list: %A" [(1, "a");(2, "b");(3, "c")];;
a list: [(1, "a"); (2, "b"); (3, "c")]
val it : unit = ()
> printfn "a signed int [%d] and a string [%s]" -2 "xyz";;
a signed int [-2] and a string [xyz]
val it : unit = ()
有关 printfn 及其占位符的更多信息,我推荐此站点:
http://fsharpforfunandprofit.com/posts/printf/
字符串“\n”与占位符没有直接关系。它会插入一个新行。
在 Microsoft Visual Studio 2015 的 F# 教程中有此代码,但略有不同。
module Integers =
/// A list of the numbers from 0 to 99
let sampleNumbers = [ 0 .. 99 ]
/// A list of all tuples containing all the numbers from 0 to 99 andtheir squares
let sampleTableOfSquares = [ for i in 0 .. 99 -> (i, i*i) ]
// The next line prints a list that includes tuples, using %A for generic printing
printfn "The table of squares from 0 to 99 is:\n%A" sampleTableOfSquares
System.Console.ReadKey() |> ignore
此代码 returns 标题 The table of squares from 0 to 99 is:
。
然后它发送 1-99 的数字及其方块。我不明白为什么需要 \n%A
,特别是为什么它必须是 A.
以下是一些其他类似的例子,但字母不同:
- 使用
%d
module BasicFunctions =
// Use 'let' to define a function that accepts an integer argument and returns an integer.
let func1 x = x*x + 3
// Parenthesis are optional for function arguments
let func1a (x) = x*x + 3
/// Apply the function, naming the function return result using 'let'.
/// The variable type is inferred from the function return type.
let result1 = func1 4573
printfn "The result of squaring the integer 4573 and adding 3 is %d" result1
- 使用
%s
module StringManipulation =
let string1 = "Hello"
let string2 = "world"
/// Use @ to create a verbatim string literal
let string3 = @"c:\Program Files\"
/// Using a triple-quote string literal
let string4 = """He said "hello world" after you did"""
let helloWorld = string1 + " " + string2 // concatenate the two strings with a space in between
printfn "%s" helloWorld
/// A string formed by taking the first 7 characters of one of the result strings
let substring = helloWorld.[0..6]
printfn "%s" substring
这让我有点困惑,因为他们必须要有他们的信件,否则他们将无法工作,有人也可以。请解释 %a
、%d
和 %s
以及任何其他(如果有的话)以及 \n
的含义。
本页对此进行了全部解释:https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/plaintext-formatting
F# 使用类似于 C 的合理标准格式字符串。
总结一下:
%s prints a string
%d is an integer
唯一奇怪的是%A
,它会尝试打印任何东西
\n
只是一个换行符
符号 %A、%s 和 %d 是占位符。当通过其参数值调用函数 printfn 时,它们将被替换。每个占位符都需要一个特定的参数类型:
- %s - 字符串
- %d - 有符号整数
- %A - pretty-printing 元组、记录和联合类型
以下是一些可以在 F# REPL 中执行的示例:
> printfn "a string: %s" "abc";;
a string: abc
val it : unit = ()
> printfn "a signed int: %d" -2;;
a signed int: -2
val it : unit = ()
> printfn "a list: %A" [1;2;3];;
a list: [1; 2; 3]
val it : unit = ()
> printfn "a list: %A" [(1, "a");(2, "b");(3, "c")];;
a list: [(1, "a"); (2, "b"); (3, "c")]
val it : unit = ()
> printfn "a signed int [%d] and a string [%s]" -2 "xyz";;
a signed int [-2] and a string [xyz]
val it : unit = ()
有关 printfn 及其占位符的更多信息,我推荐此站点:
http://fsharpforfunandprofit.com/posts/printf/
字符串“\n”与占位符没有直接关系。它会插入一个新行。