Skip to content

Instantly share code, notes, and snippets.

@shubhamkumar13
Created October 12, 2020 00:44
Show Gist options
  • Save shubhamkumar13/c9d033de08943b4140ce9f16b2dde94d to your computer and use it in GitHub Desktop.
Save shubhamkumar13/c9d033de08943b4140ce9f16b2dde94d to your computer and use it in GitHub Desktop.
Transpose in ocaml
let matrix_hd = fun matrix -> List.fold_left (fun acc x -> (List.hd x) :: acc) [] matrix |> List.rev
let matrix_tl = fun matrix -> List.fold_left (fun acc x -> (List.tl x) :: acc) [] matrix |> List.rev
let transpose = fun matrix ->
let rec loop = fun acc matrix ->
match matrix with
| hd :: tl when (List.length hd) = 0 -> acc
| x -> loop ((matrix_hd x) :: acc) (matrix_tl x) in
loop [] matrix |> List.rev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment