[转]F# Samples 101 - Visual Studio 2010
生活随笔
收集整理的這篇文章主要介紹了
[转]F# Samples 101 - Visual Studio 2010
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://code.msdn.microsoft.com/F-Samples-101-0576cb9f/sourcecode?fileId=18956&pathId=1045958806
?
// F# 教程文件 // // 此文件包含代碼示例來引導您了解 // F# 語言的基元。 // // 在 http://fsharp.net 網站上了解有關 F# 的更多信息 // // 有關更大的 F# 示例集合,請參見: // http://go.microsoft.com/fwlink/?LinkID=124614 // // 內容: // - 簡單計算 // - 針對整數的函數 // - 元組 // - 布爾值 // - 字符串 // - 列表 // - 數組 // - 更多集合 // - 函數 // - 類型: 聯合 // - 類型: 記錄 // - 類型: 類 // - 類型: 接口 // - 類型: 帶有接口實現的類 // - 輸出// 打開某些標準命名空間 open System// 簡單計算 // --------------------------------------------------------------- // 此處是一些簡單的計算。請注意可以如何通過“///”注釋來 // 說明代碼。將鼠標指針懸停在任何變量引用的上面, // 可查看相關文檔。/// 一個非常簡單的整數常量 let int1 = 1/// 另一個非常簡單的整數常量 let int2 = 2/// 將兩個整數相加 let int3 = int1 + int2// 針對整數的函數 // ---------------------------------------------------------------/// 一個針對整數的函數 let f x = 2*x*x - 5*x + 3/// 簡單計算的結果 let result = f (int3 + 4)/// 另一個針對整數的函數 let increment x = x + 1/// 計算一個整數的階乘 let rec factorial n = if n=0 then 1 else n * factorial (n-1)/// 計算兩個整數的最大公因數 let rec hcf a b = // 注意: 兩個參數之間由空格分隔if a=0 then belif a<b then hcf a (b-a) // 注意: 兩個參數之間由空格分隔else hcf (a-b) b// 注意: 函數參數通常是由空格分隔的// 注意:“let rec”定義一個遞歸函數// 元組 // ---------------------------------------------------------------// 一個包含整數的簡單元組 let pointA = (1, 2, 3)// 一個包含整數、字符串和雙精度浮點數的簡單元組 let dataB = (1, "fred", 3.1415)/// 一個用于交換兩個值在元組中的順序的函數 let Swap (a, b) = (b, a)// 布爾值 // ---------------------------------------------------------------/// 一個簡單的布爾值 let boolean1 = true/// 另一個簡單的布爾值 let boolean2 = false/// 使用 AND、OR 和 NOT 計算一個新布爾值 let boolean3 = not boolean1 && (boolean2 || false)// 字符串 // ---------------------------------------------------------------/// 一個簡單字符串 let stringA = "Hello"/// 另一個簡單字符串 let stringB = "world"/// 使用字符串串聯計算的“Hello world” let stringC = stringA + " " + stringB/// 使用 .NET 庫函數計算的“Hello world” let stringD = String.Join(" ",[| stringA; stringB |])// 嘗試重新鍵入上面的行以查看 Intellisense 是如何起作用的// 注意,針對(部分)標識符按 Ctrl-J 時將重新激活它// 功能列表 // ---------------------------------------------------------------/// 空列表 let listA = [ ] /// 包含 3 個整數的列表 let listB = [ 1; 2; 3 ] /// 包含 3 個整數的列表,注意,:: 是“cons”運算 let listC = 1 :: [2; 3] /// 使用遞歸函數計算一個整數列表的總和 let rec SumList xs =match xs with| [] -> 0| y::ys -> y + SumList ys/// 列表的總和 let listD = SumList [1; 2; 3] /// 介于 1 和 10 之間的整數(含 1 和 10)的列表 let oneToTen = [1..10]/// 前 10 個整數的平方 let squaresOfOneToTen = [ for x in 0..10 -> x*x ]// 可變數組 // ---------------------------------------------------------------/// 創建一個數組 let arr = Array.create 4 "hello" arr.[1] <- "world" arr.[3] <- "don"/// 通過對數組對象使用實例方法計算數組的長度 let arrLength = arr.Length // 使用切片表示法提取子數組 let front = arr.[0..2]// 更多集合 // ---------------------------------------------------------------/// 一個包含整數鍵和字符串值的字典 let lookupTable = dict [ (1, "One"); (2, "Two") ]let oneString = lookupTable.[1]// 有關其他一些常見的數據結構,請參見: // System.Collections.Generic // Microsoft.FSharp.Collections // Microsoft.FSharp.Collections.Seq // Microsoft.FSharp.Collections.Set // Microsoft.FSharp.Collections.Map// 函數 // ---------------------------------------------------------------/// 一個對其輸入求平方的函數 let Square x = x*x // 跨值列表映射函數 let squares1 = List.map Square [1; 2; 3; 4] let squares2 = List.map (fun x -> x*x) [1; 2; 3; 4]// 管線 let squares3 = [1; 2; 3; 4] |> List.map (fun x -> x*x) let SumOfSquaresUpTo n = [1..n] |> List.map Square |> List.sum// 類型: 聯合 // ---------------------------------------------------------------type Expr = | Num of int| Add of Expr * Expr| Mul of Expr * Expr| Var of stringlet rec Evaluate (env:Map<string,int>) exp = match exp with| Num n -> n| Add (x,y) -> Evaluate env x + Evaluate env y| Mul (x,y) -> Evaluate env x * Evaluate env y| Var id -> env.[id]let envA = Map.ofList [ "a",1 ;"b",2 ;"c",3 ]let expT1 = Add(Var "a",Mul(Num 2,Var "b")) let resT1 = Evaluate envA expT1// 類型: 記錄 // ---------------------------------------------------------------type Card = { Name : string;Phone : string;Ok : bool }let cardA = { Name = "Alf" ; Phone = "(206) 555-0157" ; Ok = false } let cardB = { cardA with Phone = "(206) 555-0112"; Ok = true } let ShowCard c = c.Name + " Phone: " + c.Phone + (if not c.Ok then " (unchecked)" else "")// 類型: 類 // ---------------------------------------------------------------/// 二維矢量 type Vector2D(dx:float, dy:float) = // 預先計算的矢量長度let length = sqrt(dx*dx + dy*dy)/// 沿 X 軸的位移member v.DX = dx/// 沿 Y 軸的位移member v.DY = dy/// 矢量的長度member v.Length = length// 按某個常量重新縮放矢量member v.Scale(k) = Vector2D(k*dx, k*dy)// 類型: 接口 // ---------------------------------------------------------------type IPeekPoke = abstract Peek: unit -> intabstract Poke: int -> unit// 類型: 帶有接口實現的類 // ---------------------------------------------------------------/// 一個用于計算其被發送的次數的小組件 type Widget(initialState:int) = /// 該小組件的內部狀態let mutable state = initialState// 實現 IPeekPoke 接口interface IPeekPoke with member x.Poke(n) = state <- state + nmember x.Peek() = state /// 是否已發送該小組件?member x.HasBeenPoked = (state <> 0)let widget = Widget(12) :> IPeekPokewidget.Poke(4) let peekResult = widget.Peek()// 輸出 // ---------------------------------------------------------------// 輸出一個整數 printfn "peekResult = %d" peekResult // 對一般輸出使用 %A 來輸出結果 printfn "listC = %A" listC?
轉載于:https://www.cnblogs.com/freeliver54/p/3941105.html
總結
以上是生活随笔為你收集整理的[转]F# Samples 101 - Visual Studio 2010的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: setTimeout延时0毫秒的作用
- 下一篇: 01 MySQL锁概述