F#测试记录代码.

1.动态得到类型.

#light
open System.Threading
open System

type Address = { Name : string;Zip : int}
let getType (x : obj) =
    match x with
    | :? string -> "x is a string"
    | :? int -> "x is a int"
    | :? System.Exception -> "x is an exception"
    | :? Address -> "x is a Address"
    | :? _ -> "invalid type"   

getType {Name = "2edd" ;Zip = 3} |> Console.WriteLine
Console.ReadKey true

2.http://www.cnblogs.com/1-2-3/archive/2010/03/02/grasp-algorithm1.html插入排序算法改个F#版.

let InsertionSort (s:int[]) =
    for index = 0 to s.Length - 2 do
         let b = s.[index + 1] in
         let j = ref index
         while !j>=0 && s.[!j] > b do
            s.[!j + 1] <- s.[!j]
            j := !j - 1
         s.[!j + 1] <- b
    s  
    
let cc = InsertionSort [| 4;2;5;10;7 |] 

其中for .. to ..to 相当于>=.操作符+和数字之间空格不要忘记.

 

3.得到相关类型的所有方法.

4.测试下.

posted @ 2010-02-25 15:36  天天不在  阅读(285)  评论(0编辑  收藏  举报