Skip to content

Instantly share code, notes, and snippets.

@faiface
Created March 14, 2025 20:35
Show Gist options
  • Save faiface/5a9561610eaca5117cf38f8a0af3c89e to your computer and use it in GitHub Desktop.
Save faiface/5a9561610eaca5117cf38f8a0af3c89e to your computer and use it in GitHub Desktop.
type Nat = recursive either {
.zero!
.succ self
}
type List<T> = recursive either {
.empty!
.item(T) self
}
// sorts the argument from least to greatest
dec bubble_sort : [List<Nat>] List<Nat>
def bubble_sort = [numbers] numbers begin {
.empty! => .empty!
.item(x) xs => do {
let (min, xs)! = extract_min(x, xs)
let sorted_xs = xs loop
} in .item(min) sorted_xs
}
dec sort_two : [Nat, Nat] (Nat, Nat)!
def sort_two = [a, b] a begin {
.zero! => (.zero!, b)!
.succ a => b {
.zero! => (.zero!, .succ a)!
.succ b => let (lo, hi)! = a loop
in (.succ lo, .succ hi)!
}
}
dec extract_min : [Nat, List<Nat>] (Nat, List<Nat>)!
def extract_min = [x, xs] xs begin {
.empty! => (x, .empty!)!
.item(y) ys => do {
let (min, ys)! = let x = y in ys loop
let (lo, hi)! = sort_two(x, min)
} in (lo, .item(hi) ys)!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment