-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cookbook : and Sorting list and arrays (#2098)
* Cookbook: add Sorting * Update 00-stdlib.md / remove tabs * Update 00-stdlib.md / remove tabs --------- Co-authored-by: Frederic LOYER <[email protected]>
- Loading branch information
1 parent
aba4a31
commit a6f6d2a
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
packages: [] | ||
sections: | ||
- filename: main.ml | ||
language: ocaml | ||
code_blocks: | ||
- explanation: | | ||
Sorting a list of item, returning a sorted copy. | ||
code: | | ||
let l = [ 1; 90; 42; 27 ];; | ||
let l' = List.sort compare a | ||
- explanation: | | ||
Sorting an array while modifying it. | ||
code: | | ||
let a = [| 1; 90; 42; 27 |];; | ||
let () = Array.sort compare a;; | ||
- explanation: | | ||
Defining a custom `compare` function (here a case insensitive string comparison) an using it to compare. | ||
Note: the comparison function applied to `a` and `b` should return 1 if `a` is greater, 0, if equal, -1 if lower than `b`. | ||
code: | | ||
let compare_insensitive a b = | ||
compare (String.lowercase_ascii a) (String.lowercase_ascii b) | ||
let a = [| "ABC"; "BCD"; "abc"; "bcd" |] | ||
let () = Array.compare_insensitive a | ||
--- | ||
|