FSharp Functional Programming—Literals


Jump to: navigation, search
CSharp-Online.NET:Articles
.NET Articles

F# Functional Programming

© 2007 Robert Pickering

Literals

Literals represent constant values and are useful building blocks for computations. F# has a rich set of literals, summarized in Table 3-1.

Table 3-1. F# Literals

Example F# Type .NET Type Description
"Hello\t ", "World\n" string System.String A string in which a backslash (\) is an escape character
@"c:\dir\fs", @"""" string System.String A verbatim string where a backslash (\) is a regular character
"bytesbytesbytes"B byte array System.Byte[] A string that will be stored as a byte array
'c' char System.Char A character
true, false bool System.Boolean A Boolean
0x22 int/int32 System.Int32 An integer as a hexadecimal
0o42 int/int32 System.Int32 An integer as an octal
0b10010 int/ int32 System.Int32 An integer as a binary
34y sbyte System.SByte A signed byte
34uy byte System.Byte An unsigned byte
34s int16 System.Int16 A 16-bit integer
34us uint16 System.UInt16 An unsigned 16-bit integer
34l int/int32 System.Int32 A 32-bit integer
34ul uint32 System.UInt32 An unsigned 32-bit integer
34n nativeint System.IntPtr A native-sized integer
34un unativeint System.UIntPtr An unsigned native-sized integer
34L int64 System.Int64 A 32-bit integer
34UL uint64 System.Int64 An unsigned 32-bit integer
3.0F, 3.0f float32 System.Single A 32-bit IEEE floating-point number
3.0 float System.Double A 64-bit IEEE floating-point number
3474262622571I bigint Microsoft.FSharp.Math.BigInt An arbitrary large integer
474262612536171N bignum Microsoft.FSharp.Math.BigNum An arbitrary large number

In F# string literals can contain newline characters, and regular string literals can contain standard escape codes. Verbatim string literals use a backslash (\) as a regular character, and two double quotes ("") are the escape for a quote. You can define all integer types using hexadecimal and octal by using the appropriate prefix and postfix. The following example shows some of these literals in action, along with how to use the F# printf function with a %A pattern to output them to the console. The printf function interprets the %A format pattern using a combination of F#’s reflection and the .NET ToString method, which is available for every type, to output values in a readable way. You can also access this functionality by using the print_any and any_to_string functions from the F# library.

#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()

The results of this example, when compiled and executed, are as follows:


"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



Previous_Page_.gif Next_Page_.gif


Personal tools