Skip to content

Commit

Permalink
Cookbook : and Sorting list and arrays (#2098)
Browse files Browse the repository at this point in the history
* 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
2 people authored and Cuihtlauac ALVARADO committed Apr 25, 2024
1 parent aba4a31 commit a6f6d2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion data/cookbook/cookbook_categories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ cookbook_categories:
tasks: []
- title: Sorting
folder: sorting
tasks: []
tasks:
- title: Sorting Lists and Arrays
folder: sorting
- title: Text Processing
folder: text-processing
tasks: []
Expand Down
26 changes: 26 additions & 0 deletions data/cookbook/sorting/sorting/00-stdlib.md
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
---

0 comments on commit a6f6d2a

Please sign in to comment.