F# 学习笔记(列表)

列表,F#里的列表也是递归结构,这种递归结构让语法更加的清晰和易懂

[<EntryPoint>]
let main argv = 
    let list1 = [1;3;5]
    list1 |> printfn "%A"
    let list2 = [2..2..10]
    list2 |> printfn "%A"
    let list3 = [for i in 1..4 -> i*i*i]
    list3 |> printfn "%A"
    let x = list1.[0]
    printfn "%i" x
    let len1,len2 = list1.Length,list2.Length
    printfn "%i,%i" len1 len2
    let el = []
    (el.Length = 0) |> printfn "%b"
    let b = ([1;3;5] <> [5;3;1])
    b |> printfn "%b"
    //list1.[1] <- 7 //列表元素不可变,这里hi出错
    let list4 = List.append list1 list2
    list4 |> printfn "%A"
    let list44 = list1 @ list2
    list44 |> printfn "%A"
    let list5 = list1 @ [8;10] @ list3
    list5 |> printfn "%A"
    let sL1 = ["北京";"济南";"上海";"杭州";"温州";"福州"]
    let start = sL1.Head
    let routel = sL1.Tail
    start |> printfn "%s"
    let aL = [1]
    printfn "Head: %O, Tail: %O" aL.Head aL.Tail
    let sL2 = "天津" :: routel
    let sL3 = start :: ["南昌";"福州"]
    sL2 |> printfn "%A"
    sL3 |> printfn "%A"
    let sL4 = "西安":: "郑州" :: sL3.Tail
    sL4 |> printfn "%A"
    let htt l1 =
        match l1 with
        | [] -> []
        | [_] -> l1
        | head :: tail -> (tail @ [head])
    (htt [1..5]) |> printfn "%A"
    (htt [100]) |> printfn "%A"
    let rec cas (l1 : string list) =
        match l1 with
        | [] -> ""
        | [_] -> l1.Head
        | head :: tail -> head + " " + (cas tail)
    (cas ["北京";"海淀";"中关村"]) |> printfn "%A"

    let rec match2 l1 a b =
        match l1 with
        | [] | [_] -> false
        | x :: y :: tail -> (x=a && y=b) || (y=a && tail <> [] && tail.Head=b) || (match2 tail a b)
    (match2 [1..5] 1 2) |> printfn "%b"
    printfn "%b,%b" (match2 [1..5] 2 4) (match2 [1..5] 4 5)
    let tpL1 = List.zip [2..8] [for i in 2..8 -> (float i) ** (10.0-float i)]
    tpL1 |> printfn "%A"
    let _, fL1 = List.unzip tpL1
    fL1 |> printfn "%A"
    let y = List.max fL1, List.min fL1, List.average fL1
    y |> printfn "%A"
    let fL2 = List.rev fL1
    fL2 |> printfn "%A"
    let fArr1 = Array.ofList fL1
    fArr1 |> printfn "%A"
    let fArr2 = Array.toList (Array.sub fArr1 0 4)
    fArr2 |> printfn "%A"

    0 // 返回整数退出代码

其实和scala没有什么太大的区别

高阶列表:

[<EntryPoint>]
let main argv = 
    let hL1 = [[1;2;3;5];[8;13;21];[34;55]]
    hL1.[0] |> printfn "%A"
    let x1 = hL1.[1].[1]
    let iArr1 = List.toArray hL1
    iArr1 |> printfn "%A"
    let LLToArr(l1: int list list) =
        let arr = Array.create l1.Length [|0|]
        for i in 0..l1.Length-1 do
            arr.[i] <- List.toArray l1.[i]
        arr
    let iArr2 = LLToArr [[1;2;3;5];[8;13;21];[34;55]]
    0 // 返回整数退出代码

今天还的干其他的,就学到这里

你可能感兴趣的:(学习笔记)