6 // For dynmic typing. To print anything in our case.
7 let inline print
a = printfn
"%A" a
9 // Write something on a file.
10 let appendFile (filename
: string) (text
: string) =
11 use sw = new StreamWriter(filename
, true)
14 // Recursive function.
19 // Mutual recursive functions.
29 // Algebraic data type.
41 | NumCard of int * Suit
42 member this
.Value = // Property.
48 | NumCard (n, _
) when n >= 2 && n <= 10 -> n
49 | NumCard (_
) -> failwith
"Card has invalid value!"
53 for suit
in [ Spade; Club; Heart; Diamond ] do
59 yield
NumCard(n, suit
)
63 type Person = { First: string; Last: string; Age: int }
64 let steve = { First = "Steve"; Last = "Holt"; Age = 17 }
65 let steves'twin = { steve with First = "Paul" }
67 // Queries. Should be used with SQL or other external data sources.
68 let youngPersonNames persons =
75 // Infinite lazy sequence.
78 for i in 1 .. System.Int32.MaxValue do
89 yield! [1; 2; 3] // Yields a collection.
93 Seq.unfold (fun a -> if a > 10 then None else Some(10 * a, a + 1)) 1
95 // Functions composition.
98 Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories)
100 >> Array.map (fun file -> (new FileInfo(file)).Length)
105 let Bill = "Bill Gates" // Must begin with a capital!
108 | Bill -> "Hello rich Bill!"
109 | name -> sprintf "Hello poor %s" name
114 | a, b when a <> b -> true