%A在F#中是什么意思?
在Microsoft Visual Studio 2015的F#教程中,有这样的代码,尽管略有不同%A在F#中是什么意思?,f#,string-formatting,F#,String Formatting,在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 sample
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
此代码返回标题从0到99的正方形表是:
然后它发送1-99的数字和它们的平方。我不明白为什么需要
\n%A
,特别是为什么它必须是A
以下是其他一些字母不同的类似示例:
%d
模块基本功能=
//使用“let”定义接受整数参数并返回整数的函数。
设func1x=x*x+3
//括号对于函数参数是可选的
设func1a(x)=x*x+3
///应用函数,使用“let”命名函数返回结果。
///变量类型是从函数返回类型推断出来的。
设result1=func1 4573
printfn“将整数4573平方后加3的结果为%d”结果1
%s
模块字符串操作=
让string1=“你好”
让string2=“世界”
///使用@创建逐字字符串文字
让string3=@“c:\Program Files\”
///使用三引号字符串文字
让string4=“”他在你之后说“你好,世界”
让helloWorld=string1+“”+string2//将两个字符串连接起来,中间留有空格
printfn“%s”helloWorld
///由一个结果字符串的前7个字符组成的字符串
让substring=helloWorld[0..6]
printfn“%s”子字符串
这让我有点困惑,因为他们必须给他们写信,否则他们将无法工作,所以有人可以。请解释
%a
、%d
和%s
以及任何其他(如果有的话)以及\n
的含义。本页对此进行了详细说明:
F#使用了与C类似的标准格式字符串
总结如下:
%s prints a string
%d is an integer
唯一奇怪的是%A
,它会尝试打印任何东西
\n
只是一个换行符符号%a、%s和%d是占位符。当函数printfn被其参数值调用时,它们将被替换。每个占位符需要一个特定的参数类型:
- %s字串
- %d符号整数
- %A-漂亮的打印元组、记录和联合类型
字符串“\n”与占位符没有直接关系。它插入了一行新词。Read怎么样?向下投票。这个问题没有显示任何研究成果。
> 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 = ()