【发布时间】:2015-12-06 00:17:47
【问题描述】:
我不知道如何在 F# 中实现 Zip 功能。谁能告诉我我做错了什么?这是我在fsi.exe 中输入的内容:
> let rec zip xs ys =
- match xs with
- | [] -> []
- | head :: tail -> (match ys with
- | [] -> []
- | headY :: tailY -> (head, headY) :: zip tail tailY);;
val zip : xs:'a list -> ys:'b list -> ('a * 'b) list
> zip [1;2;3;4] ["a","b","c","d"];;
val it : (int * (string * string * string * string)) list =
[(1, ("a", "b", "c", "d"))]
【问题讨论】:
-
替代方案:让 rec zip2 xs ys = 匹配 xs,ys 和 |xh::xt,yh::yt -> (xh,yh)::(zip2 xt yt) |_,_ - > []dotnetfiddle.net/9TkL3k
标签: f# functional-programming f#-interactive