Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created December 19, 2021 19:27
Show Gist options
  • Save ebresafegaga/bff4e2db8d71f57bdde4c6ed0f3d8358 to your computer and use it in GitHub Desktop.
Save ebresafegaga/bff4e2db8d71f57bdde4c6ed0f3d8358 to your computer and use it in GitHub Desktop.
Traspose now!
let rec transpose xss =
match xss with
| [] :: _ | [] -> []
| xs :: xss ->
let hds = List.map List.hd xss in
let tls = List.map List.tl xss in
let hd = List.hd xs in
let tl = List.tl xs in
(hd :: hds) :: transpose (tl :: tls)
let rec transpose1 xss =
match xss with
| [] -> [] (* we would only get here if we're actually given an empty list *)
| [xs] -> xs |> List.map (fun elem -> [elem])
| xs :: xss ->
let almost = transpose1 xss in
List.combine xs almost
|> List.map (fun (x, xs) -> x :: xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment