Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
6
Task/Reverse-a-string/OCaml/reverse-a-string-1.ocaml
Normal file
6
Task/Reverse-a-string/OCaml/reverse-a-string-1.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let string_rev s =
|
||||
let len = String.length s in
|
||||
String.init len (fun i -> s.[len - 1 - i])
|
||||
|
||||
let () =
|
||||
print_endline (string_rev "Hello world!")
|
||||
15
Task/Reverse-a-string/OCaml/reverse-a-string-2.ocaml
Normal file
15
Task/Reverse-a-string/OCaml/reverse-a-string-2.ocaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
let rev_bytes bs =
|
||||
let last = Bytes.length bs - 1 in
|
||||
for i = 0 to last / 2 do
|
||||
let j = last - i in
|
||||
let c = Bytes.get bs i in
|
||||
Bytes.set bs i (Bytes.get bs j);
|
||||
Bytes.set bs j c;
|
||||
done
|
||||
|
||||
let () =
|
||||
let s = Bytes.of_string "Hello World" in
|
||||
rev_bytes s;
|
||||
print_bytes s;
|
||||
print_newline ();
|
||||
;;
|
||||
9
Task/Reverse-a-string/OCaml/reverse-a-string-3.ocaml
Normal file
9
Task/Reverse-a-string/OCaml/reverse-a-string-3.ocaml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
let rec revs_aux strin list index =
|
||||
if List.length list = String.length strin
|
||||
then String.concat "" list
|
||||
else revs_aux strin ((String.sub strin index 1)::list) (index+1)
|
||||
|
||||
let revs s = revs_aux s [] 0
|
||||
|
||||
let () =
|
||||
print_endline (revs "Hello World!")
|
||||
Loading…
Add table
Add a link
Reference in a new issue