本文共 5631 字,大约阅读时间需要 18 分钟。
在本系列的第二部分(函数式编程、、)中,我们了解了如何使用F#进行纯粹的函数式编程。但是在一些情况下,比如I/O,几乎不能避免改变状态,也就是说会带来side effect。F#并不强求你以无状态的方式编写程序,它提供了可修改(mutable)的标识符来解决这类问题,同时它还提供了其它的程序结构以支持命令式编程。现在就来对这些特性探个究竟。
首先是unit类型,这种类型表示“没有值”。然后是F#如何处理可修改的值。最后来看看如何在F#中使用.NET类库,包括如何调用静态方法、创建对象并使用其成员、使用类的索引器和事件以及F#中的|>操作符。unit类型 没有参数和返回值的函数的类型为unit,它类似于C#中的void,或者说CLR中的System.Void。对于使用函数式编程语言的开发人员来说,不接受参数、无返回值的函数没多大意义。而在命令式编程中,由于side effect的存在,这样的函数仍有意义。unit类型表示为一对括号“()”。F# Code let main() = ()
F# Code let () = main() // -- or -- main()
F# Code let poem() = print_endline "I sent thee late a rosy wreath" print_endline "Not so much honouring thee" print_endline "As giving it a hope that there" print_endline "It could not withered be" poem()
F# Code #light let getString() = "foo bar" let _ = getString() // -- or -- ignore(getString()) // -- or -- getString() |> ignore
F# Code let mutable phrase = "Good good study, " print_endline phrase phrase <- "day day up." print_endline phrase
Output Good good study, day day up.
F# Code let redefineX() = let x = "One" printfn "Redefining: \r\nx = %s" x if true then let x = "Two" printfn "x = %s" x else () printfn "x = %s" x let mutableX() = let mutable x = "One" printfn "Mutating: \r\nx = %s" x if true then x <- "Two" printfn "x = %s" x else () printfn "x = %s" x redefineX() mutableX()
Output Redefining: X = One X = Two X = One Mutating: X = One X = Two X = Two
F# Code type Couple = {her : string; mutable him : string} let couple = {her = "Elizabeth Taylor"; him = "Nicky Hilton"} let print o = printf "%A \r\n" o let changeCouple() = print couple; couple.him <- "Michael Wilding"; print couple; couple.him <- "Michael Todd"; print couple; changeCouple()
F# Code let ref x = { contents = x } let (:=) x y = x.contents <- y let (!) x = x.contents
F# Code let phrase = ref "Inconsistency"
C# Code static int TotalArray(int[] array) { int total = 0; foreach (int element in array) { total += element; } return total; }
F# Code let totalArray (intArray : int array) = let x = ref 0 for n in intArray do x := !x + n !x
F# Code // 定义 let rhymeArray = [| "Hello"; "F#" |] // 读取 let firstPiggy = rhymeArray.[0] let secondPiggy = rhymeArray.[1] // 写入 rhymeArray.[0] <- "Byebye" rhymeArray.[1] <- "my friend" // 输出 print_endline firstPiggy print_endline secondPiggy print_any rhymeArray
F# Code let jaggedArray = [| [| "one" |]; [| "two"; "three" |] |] let singleDimension = jaggedArray.[0] let itemOne = singleDimension.[0] let itemTwo = jaggedArray.[1].[0] printfn "%s %s" itemOne itemTwo // one two
F# Code let square = Array2.create 2 2 0 square.[0,0] <- 1 square.[0,1] <- 2 square.[1,0] <- 3 square.[1,1] <- 4 printf "%A \r\n" square // [| [|1; 2|]; [|3; 4|] |]
F# Code let chars = [|'1' .. '9'|] let squares = [| for x in 1 .. 9 -> x, x * x |] printfn "%A" chars printfn "%A" squares
参考:
《Foundations of F#》 by Robert Pickering 《Expert F#》 by Don Syme , Adam Granicz , Antonio Cisternino 《》本文转自一个程序员的自省博客园博客,原文链接:http://www.cnblogs.com/anderslly/archive/2008/09/25/fs-adventure-ip-part-one.html,如需转载请自行联系原作者。