langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
31
Task/Matrix-transposition/OCaml/matrix-transposition-1.ocaml
Normal file
31
Task/Matrix-transposition/OCaml/matrix-transposition-1.ocaml
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
open Bigarray
|
||||
|
||||
let transpose b =
|
||||
let dim1 = Array2.dim1 b
|
||||
and dim2 = Array2.dim2 b in
|
||||
let kind = Array2.kind b
|
||||
and layout = Array2.layout b in
|
||||
let b' = Array2.create kind layout dim2 dim1 in
|
||||
for i=0 to pred dim1 do
|
||||
for j=0 to pred dim2 do
|
||||
b'.{j,i} <- b.{i,j}
|
||||
done;
|
||||
done;
|
||||
(b')
|
||||
;;
|
||||
|
||||
let array2_display print newline b =
|
||||
for i=0 to Array2.dim1 b - 1 do
|
||||
for j=0 to Array2.dim2 b - 1 do
|
||||
print b.{i,j}
|
||||
done;
|
||||
newline();
|
||||
done;
|
||||
;;
|
||||
|
||||
let a = Array2.of_array int c_layout [|
|
||||
[| 1; 2; 3; 4 |];
|
||||
[| 5; 6; 7; 8 |];
|
||||
|]
|
||||
|
||||
array2_display (Printf.printf " %d") print_newline (transpose a) ;;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
let rec transpose m =
|
||||
assert (m <> []);
|
||||
if List.mem [] m then
|
||||
[]
|
||||
else
|
||||
List.map List.hd m :: transpose (List.map List.tl m)
|
||||
Loading…
Add table
Add a link
Reference in a new issue