F#基础教程 直接量

  直接量代表的常数值,类似于命令式编程中的定值。F#中有一套丰富的直接量。表3-1总结了一些。

   Table 3-1.F# 直接量

例子  F# 类型  .NET 类型    描述
 "Hello\t ", "World\n"  string  System.String  一个字符串
(\) 是转义字符
 @"c:\dir\fs", @""""  string  System.String  禁止转义(\) 的方法
 "bytesbytesbytes"B  byte array  System.Byte[]  存储在字节数组中的字符串
 'c'  char  System.Char  一个字符
 true, false  bool  System.Boolean  一个布尔值
 0x22  int/int32  System.Int32  十六进制整数
 0o42  int/int32  System.Int32  八进制整数
 0b10010  int/ int32  System.Int32  二进制整数
 34y  sbyte  System.SByte  一个字节
 34uy  byte  System.Byte  一个无符号字节
 34s  int16  System.Int16  16位整数
 34us  uint16  System.UInt16  16位无符号整数
 34l  int/int32  System.Int32  32位整数
 34ul  uint32  System.UInt32  32位无符号整数
 34n  nativeint  System.IntPtr  本机大小的整数
 34un  unativeint  System.UIntPtr  本机大小无符号整数
 34L  int64  System.Int64  64位整数
 34UL  uint64  System.Int64  64位无符号整数
 3.0F, 3.0f  float32  System.Single  32位的IEEE浮点数
 3.0  float  System.Double  64位的IEEE浮点数
 3474262622571I  bigint  Microsoft.FSharp.Math.BigInt  无限大整数
 474262612536171N  bignum  Microsoft.FSharp.Math.BigNum  无限长号码

    F#的字符串可以包含换行符,可以包含转义代码。 两个双引号转义为一个引号(当前版本不可用)。下面的示例演示一些方法。以及如何使用F#中的printf函数输出内容到控制台。

#light
let message = "Hello
    World\r\n\t!"
let dir = @"c:\projects"

let bytes = "bytesbytesbytes"B

let xA = 0xFFy
let xB = 0o7777un
let xC = 0b10010UL
let print x = printfn "%A" x

let main() =
    print message;
    print dir;
    print bytes;
    print xA;
    print xB;
    print xC

main()

编译和执行结果如下:

"Hello\n           World\r\n\t!"
"c:\\projects"
[|98uy; 121uy; 116uy; 101uy; 115uy; 98uy; 121uy; 116uy; 101uy; 115uy; 98uy;
   121uy; 116uy; 101uy; 115uy|]
-1y
4095
18UL

posted @ 2011-11-21 17:21  银河系漫游指南  阅读(489)  评论(0编辑  收藏  举报