Marko
Marko

Reputation: 59

Transpose of a int list list in Ocaml

I hope that this will be quite easy for some of you. I found this solution in this forum:

let rec transpose list = match list with
| []             -> []
| []   :: xss    -> transpose xss
| (x::xs) :: xss ->
    (x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss)

This is a transposition of a list of lists and it works great.

Now I need the same this but instead of transposing an a' list list to a' list list to transpose int list list to int list list.

Thank you all!

Upvotes: 2

Views: 1180

Answers (1)

cygin
cygin

Reputation: 373

That's what polymorphism is about. The type 'a can be any type, so the solution you found works just as well with int list list.

If you really want a function whose type is int list list -> int list list, you can always force it using type annotation :

let int_transpose : int list list -> int list list = transpose

But I don't see why you would.

Upvotes: 6

Related Questions