//YooVi.Fing 翻译
// F# Tutorial File--F# 指导文件
//
// This file contains sample code to guide you through the
// primitives of the F# language. --此文件包含一些简单的代码来指导你从头到尾的了解最初的F#
//
// For a larger collection of F# samples, see:--对于想了解F#强大的集合请连接如下连接
// http://go.microsoft.com/fwlink/?LinkID=124614
//
// Contents:--以下涉及的内容
// - Simple computations--简单的计算
// - Functions on integers--关于整数的函数
// - Tuples --数组
// - Booleans--布尔
// - Strings--字符串
// - Lists--列表
// - Arrays--阵列
// - More Collections--更多集合
// - Functions--函数
// - Types: unions--联合,合并
// - Types: records--记录
// - Types: classes--类
// - Types: interfaces--界面
// - Types: classes with interface implementations--把UI和执行放在一起
// - Printing --输出
// Turn on the lightweight syntax--打开轻量的语法
#light
// open some standard namespaces--打开标准的命名空间
open System
// Simple computations--简单的技术
// ---------------------------------------------------------------
// Here are some simple computations. Note how code can be documented
// with '///' comments. Hover over any reference to a variable to
// see its documentation.--这里有一些简单的计算,记录了怎么注释代码,以及引用一个变量
/// A very simple constant integer--一个非常简单的整型常量的声明
let int1 = 1
/// A second very simple constant integer--同样
let int2 = 2
/// Add two integers--两个整型变量相加而后赋值给一个新的整型变量
let int3 = int1 + int2
// Functions on integers --整型类型的函数
// ---------------------------------------------------------------
/// A function on integers--声明函数 f,函数所需参数x
let f x = 2*x*x - 5*x + 3
/// The result of a simple computation --一个简单的计算,调用f,参数是int3+4
let result = f (int3 + 4)
/// Another function on integers--此函数 increment
let increment x = x + 1
/// Compute the factorial of an integer--阶乘用了递归 关键子 rec应该是recall的简写,意译为《递归》吧
let rec factorial n = if n=0 then 1 else n * factorial (n-1)
/// Compute the highest-common-factor of two integers--计算两个整型类型数据的最大公因子
let rec hcf a b = // notice: 2 parameters separated by spaces--注意:使用2个参数时用空格隔开
if a=0 then b
elif a<b then hcf a (b-a) // notice: 2 arguments separated by spaces--注意:使用2个参数时用空格隔开
else hcf (a-b) b
// note: function arguments are usually space separated--函数参数一般都是用空格隔开
// note: 'let rec' defines a recursive function--let rec 语法定义了一个递归函数
// Tuples--数组
// ---------------------------------------------------------------
// A simple tuple of integers --一个简单整型数组
let pointA = (1, 2, 3)
// A simple tuple of an integer, a string and a double-precision floating point number---一个数组里面可以放 整型,字符串型,浮点型
let dataB = (1, "fred", 3.1415)
/// A function that swaps the order of two values in a tuple --置换函数
let Swap (a, b) = (b, a)
// Booleans--布尔类型
// ---------------------------------------------------------------
/// A simple boolean value--布尔类型的赋值
let boolean1 = true
/// A second simple boolean value
let boolean2 = false
/// Compute a new boolean using ands, ors, and nots-- 关键字not,以及百年不变的&&,||
let boolean3 = not boolean1 && (boolean2 || false)
// Strings
// ---------------------------------------------------------------
/// A simple string--字符串赋值
let stringA = "Hello"
/// A second simple string
let stringB = "world"
/// "Hello world" computed using string concatentation--字符串的联合
let stringC = stringA + " " + stringB
/// "Hello world" computed using a .NET library function--可以使用.net类库的string.join()方法
let stringD = String.Join(" ",[| stringA; stringB |])
// Try re-typing the above line to see intellisense in action
// Note, ctrl-J on (partial) identifiers re-activates it--ctrl-J
// Functional Lists
// ---------------------------------------------------------------
/// The empty list--声明一个空列表
let listA = [ ]
/// A list with 3 integers--包含3个整型类型的数据
let listB = [ 1; 2; 3 ]
/// A list with 3 integers, note :: is the 'cons' operation--::操作符
let listC = 1 :: [2; 3]
/// Compute the sum of a list of integers using a recursive function--使用递归函数计算两个列表的整型数值
let rec SumList xs =
match xs with
| [] -> 0
| y::ys -> y + SumList ys
/// Sum of a list--列表之和
let listD = SumList [1; 2; 3]
/// The list of integers between 1 and 10 inclusive --数字1到10 包含1和10
let oneToTen = [1..10]
/// The squares of the first 10 integers--每个数的平方数
let squaresOfOneToTen = [ for x in 0..10 -> x*x ]
// Mutable Arrays
// ---------------------------------------------------------------
/// Create an array --创建一个数组--4是数组的大小,第一个值hello,给数组赋值“<-”
let arr = Array.create 4 "hello"
arr.[1] <- "world"
arr.[3] <- "don"
/// Compute the length of the array by using an instance method on the array object--计算数组的的长度
let arrLength = arr.Length
// Extract a sub-array using slicing notation--取数组arr的一部分arr.[1..2]
let front = arr.[0..2]
// More Collections--更多集合
// ---------------------------------------------------------------
/// A dictionary with integer keys and string values--一个包含【整型的键,字符串类型的值】的dictionary
let lookupTable = dict [ (1, "One"); (2, "Two") ]
let oneString = lookupTable.[1]
// For some other common data structures, see:---对于想看其他常用数据结构请在如下命名空间中查询
// System.Collections.Generic
// Microsoft.FSharp.Collections
// Microsoft.FSharp.Collections.Seq
// Microsoft.FSharp.Collections.Set
// Microsoft.FSharp.Collections.Map
// Functions
// ---------------------------------------------------------------
/// A function that squares its input--一个用于计算平方数的函数
let Square x = x*x
// Map a function across a list of values--
let squares1 = List.map Square [1; 2; 3; 4]
let squares2 = List.map (fun x -> x*x) [1; 2; 3; 4]
// Pipelines--
let squares3 = [1; 2; 3; 4] |> List.map (fun x -> x*x)
let SumOfSquaresUpTo n =
[1..n]
|> List.map Square
|> List.sum
// Types: unions
// ---------------------------------------------------------------
type Expr =
| Num of int
| Add of Expr * Expr
| Mul of Expr * Expr
| Var of string
let 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.of_list [ "a",1 ;
"b",2 ;
"c",3 ]
let expT1 = Add(Var "a",Mul(Num 2,Var "b"))
let resT1 = Evaluate envA expT1
// Types: records
// ---------------------------------------------------------------
type Card = { Name : string;
Phone : string;
Ok : bool }
let cardA = { Name = "Alf" ; Phone = "(206) 555-8257" ; Ok = false }
let cardB = { cardA with Phone = "(206) 555-4112"; Ok = true }
let ShowCard c =
c.Name + " Phone: " + c.Phone + (if not c.Ok then " (unchecked)" else "")
// Types: classes
// ---------------------------------------------------------------
/// A 2-dimensional vector
type Vector2D(dx:float, dy:float) =
// The pre-computed length of the vector
let length = sqrt(dx*dx + dy*dy)
/// The displacement along the X-axis
member v.DX = dx
/// The displacement along the Y-axis
member v.DY = dy
/// The length of the vector
member v.Length = length
// Re-scale the vector by a constant
member v.Scale(k) = Vector2D(k*dx, k*dy)
// Types: interfaces
// ---------------------------------------------------------------
type IPeekPoke =
abstract Peek: unit -> int
abstract Poke: int -> unit
// Types: classes with interface implementations
// ---------------------------------------------------------------
/// A widget which counts the number of times it is poked
type Widget(initialState:int) =
/// The internal state of the Widget
let mutable state = initialState
// Implement the IPeekPoke interface
interface IPeekPoke with
member x.Poke(n) = state <- state + n
member x.Peek() = state
/// Has the Widget been poked?
member x.HasBeenPoked = (state <> 0)
let widget = Widget(12) :> IPeekPoke
widget.Poke(4)
let peekResult = widget.Peek()
// Printing
// ---------------------------------------------------------------
// Print an integer
printfn "peekResult = %d" peekResult
// Print a result using %A for generic printing
printfn "listC = %A" listC
Console.WriteLine();