if System.DateTime.Now.DayOfWeek = System.DayOfWeek.Thursday then print_endline "Thursday play list: lazy afternoon" else print_endline "Alt play list: pop music"
let whitePollution = [| "This term refers to "; "pollution caused by "; "litter of used plastic bags, "; "polystyrene cups, "; "food containers and paper." |]
for index = Array.length whitePollution - 1 downto 0 do System.Console.Write whitePollution.[index]
// 压洲 let pressureContinent = ref [ "This phrase's pronunciation is "; "similar to \"Asia\" in Chinese, "; "but it means "; "a continent of pressure." ]
while (List.nonempty !pressureContinent) do System.Console.Write(List.hd !pressureContinent); pressureContinent := List.tl !pressureContinent
let file = new FileInfo("notExisting.txt") if not file.Exists then using(file.CreateText()) (fun stream -> stream.WriteLine("hello, f#")) file.Attributes <- FileAttributes.ReadOnly
open System.IO let fileName = "test.txt" let file = if File.Exists(fileName) then Some(new FileInfo(fileName, Attributes = FileAttributes.ReadOnly)) else None
#light let simpleList = [box 1; box 2.0; box "three"]
let recognizeType (item : obj) = match item with | :? System.Int32 as x -> printfn "An integer: %i" x | :? System.Double as x -> printfn "A double: %f" x | :? System.String as x -> printfn "A string: %s" x | x -> printfn "An object: %A" x
let now = System.DateTime.Now System.Console.WriteLine(now)
try if now.Second % 3 = 0 then raise (new System.Exception()) else raise (new System.ApplicationException()) with | :? System.ApplicationException -> print_endline "A second that was not a multiple of 3" | _ -> print_endline "A second that was a multiple 3"
type fsDate = { year : int; month : int; day : int } let fsDateList = [ { year = 1999; month = 12; day = 31 } { year = 2000; month = 12; day = 31 } { year = 2001; month = 12; day = 31 } ]
List.iter (fun d -> print_int d.year) fsDateList fsDateList |> List.iter (fun d -> print_int d.year)
走马观花,这一站的风景看得差不多了,命令式编程的核心部分也介绍完毕。有了函数式编程和命令式编程的知识,我们应该有信心解决大部分问题了。使用F#,我们可以选择合适的编程范式,而不是囿于特定的一种范式。下一站,我们将看到第三种主要的编程范式——面向对象编程。 参考: 《Foundations of F#》 by Robert Pickering 《Expert F#》 by Don Syme , Adam Granicz , Antonio Cisternino 《F# Specs》