Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# zip implementation

I can't figure out how to implement the Zip function in F#. Can anyone tell me what I am doing wrong? Here is what I have typed into 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"))]
like image 335
skb Avatar asked Sep 18 '25 19:09

skb


1 Answers

In your example ["a","b","c","d"] is a list that contains one element which is 4-dimensional tuple. That is why you are getting unexpected results from zip. Just use ; as elements delimiter instead.

like image 117
ntr Avatar answered Sep 22 '25 18:09

ntr