Code:
pkg install rebar3 gleam
gleam new sortme
./src/sortme.gleam
Code:
import gleam/io
import gleam/string
fn insert(element: Int, sorted_list: List(Int)) -> List(Int) {
io.print("Insert: " <> string.inspect(element) <> "::" <> string.inspect(sorted_list) <> "\n")
case sorted_list {
[] -> [element]
[head, ..tail] ->
case element <= head {
True -> {
io.print("Move to left: " <> string.inspect(element)<>"\n")
[element, head, ..tail]
}
False -> {
io.print("Move to right: " <> string.inspect(element)<>"\n")
[head, ..insert(element, tail)]
}
}
// end case element-head
}
// end case sorted_list
}
pub fn insertion_sort(list: List(Int)) -> List(Int) {
io.print("Insertion_sort: " <> string.inspect(list)<>"\n")
case list {
[] -> []
[head, ..tail] -> insert(head, insertion_sort(tail))
}
// end case list
}
pub fn main() {
let ongesorteerde_lijst = [4, 2, 7, 1, 3, 9, 0, 5, -2]
io.print("Origineel: " <> string.inspect(ongesorteerde_lijst) <> "\n")
let gesorteerde_lijst = insertion_sort(ongesorteerde_lijst)
io.print("Gesorteerd: " <> string.inspect(gesorteerde_lijst) <> "\n")
}
gleam run
That's all folks!