F#链表——List各个函数解释

使用F# 的时候老是会忘记List 里面的函数的使用方法,可能是有些的确是用的有点少,今天把这些都试了一下,弄了点注释,把结果运行结果也弄上去了,方便以后查看:)。

 说明一下:如果函数使用的List为SrcA 和SrcB,说明此函数需要的两个链表必须为等长的。如果使用的是Src 和Src2 则没有这样的限制。 

下面就贴出代码:):

let src = [1;4;2;3]
let src2 = [3;7;5;3;5]
let srcA = [1;2;3;4]
let srcB = [4;3;2;1]
let myAdd x y = 
    x + y
let operator x y z= 
    x + y + z
//比较两个列表中序号相同的一对元素是否满足给定条件,如果存在那么一对那么就返回true
//val lis : bool = true
let lis = List.exists2 (fun x y-> x = y) src src2

//将2加到头部
//val result1 : int list = [2; 1; 4; 2; 3]
let result1 = List.Cons(2,src)

//连接两个链表
//val result2 : int list = [1; 4; 2; 3; 3; 7; 5; 3; 5]
let result2 = List.append src src2

//选择使给定函数返回至为Some(x)的所有元素,返回一个链表
//val result3 : int list = [4; 3]
let result3 = List.choose(fun x -> if x > 2 then Some(x) else None) src

//对每一个元素执行一个给定函数,此给定函数将返回一个链表,最后将所有产生的链表合成一个
//val result4 : int list = [1; 1; 2; 3; 4; 1; 2; 1; 2; 3]
let result4 = List.collect( fun x -> [1..x]) src

//连接两个链表
//val result5 : int list = [1; 4; 2; 3; 3; 7; 5; 3; 5]
let result5 = List.concat [src;src2]

//返回满足过滤条件的元素链表
//val result6 : int list = [4; 2]
let result6 = List.filter(fun x -> x % 2 = 0 ) src

//选择使给定函数返回至为Some(x)的地一个元素
//val result7 : int = 4
let result7 = List.find(fun x -> x % 2 = 0) src

//选择使给定函数返回至为Some(x)的地一个元素的位置
//val result8 : int = 1
let result8 = List.findIndex(fun x -> x % 2 = 0) src

//将链表中的第一个元素与给定初始值0代入给定函数计算,计算出的结果与第二个元素一起代入给定函数计算,直到最后一个
//val result9 : int = 10
let result9 = List.fold myAdd 0 src

//must have the same length
//val result10 : int = 20
let result10 = List.fold2 operator 0 srcA srcB

//val result11 : int = 10
let result11 = List.foldBack myAdd src 0

//must have the same length
//val result12 : int = 20
let result12 = List.foldBack2 operator srcA srcB 0

//测试所有元素是否都满足给定函数的要求 
//val result13 : bool = true
let result13 = List.forall(fun x -> x > 0) src

//val result14 : bool = false
let result14 = List.forall2(fun x y -> x > y) src src2

//must have the same length 对每个元素执行给定函数
(*1,4
2,3
3,2
4,1
val result15 : unit = ()*)
let result15 = List.iter2(fun x y-> printfn "%d,%d" x y) srcA srcB

//must have the same length 对每个元素执行给定函数
//val result16 : int list = [5; 5; 5; 5]
let result16 = List.map2(fun x y -> x + y) srcA srcB

//这里的x即为元素位置
(*0,1
1,4
2,2
3,3
val result17 : unit = ()*)
let result17 = List.iteri(fun x y -> printfn "%d,%d"x y) src

//must have the same length  
(*0,1,4
1,2,3
2,3,2
3,4,1
val result18 : unit = ()*)
let result18 = List.iteri2(fun x y z -> printfn "%d,%d,%d" x y z) srcA srcB

//must have the same length
//val result19 : int list = [5; 5; 5; 5]
let result19 = List.map2(fun x y -> x + y) srcA srcB

//x为序号,从0到列表长度-1
//val result20 : int list = [1; 5; 4; 6]
let result20 = List.mapi(fun x y -> y + x) src 
(*0
1
2
3
val result21 : int list = [5; 5; 5; 5]*)
let result21 = List.mapi2(fun x y z -> printfn "%d" x; y + z ) srcA srcB

//从大于2的元素分开成两个子列表
//val result22 : int list * int list = ([4; 3], [1; 2])
let result22 = List.partition(fun x -> x > 2) src

//对列表按指定排序函数 排序
let result23 = List.permute(fun x -> x ) src

//找到第一个满足函数要求的元素,即能代入函数中返回Some(x)的元素,而List.choose则全部返回这些满足条件的数 
//val result24 : int = 4
let result24 = List.pick(fun x -> if x % 2 = 0 then Some(x) else None) src

//与List.flod不同的是:此函数不需要初始值,其它一样 
//val result25 : int = 10
let result25 = List.reduce(fun x y -> x + y) src

//val result26 : int list list = [[1; 4; 2; 3]; [1; 4; 2; 3]]
let result26 = List.replicate(2) src

//用法同flod,只是返回结果里将中间结果值也包含进去了,韩慧一个List
//val result27 : int list = [0; 1; 5; 7; 10]
let result27 = List.scan(fun x y -> x + y) 0 src

//将两个列表合并
//val result28 : (int * int) list = [(1, 4); (2, 3); (3, 2); (4, 1)]//
let result28 = List.zip srcA srcB

做个笔记:)

原文地址:https://www.cnblogs.com/FsharpZack/p/2777764.html