You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hej, while working on a merge sort for hica I did some light research but couldn't find any sorting functionality in Koka.
I wrote a version that I expose via codegen to hica.
The hica sort turned out okay, simple to use and does a top-down using list variant.
Maybe something the Koka community is interested to reuse?
Implementation
The sort is split into two functions:
merge-with — merges two already-sorted lists using a comparator
Both require the div effect because take/drop make the recursion
non-structurally decreasing.
// Merge two sorted lists using a comparison function.// `cmp(a, b)` should return True when `a` should come before `b`.funmerge-with( xs :list<a>, ys :list<a>, cmp :(a, a)->bool ) :divlist<a>match xs
Nil -> ys
Cons(x, xr) -> match ys
Nil -> xs
Cons(y, yr) ->
if cmp(x, y) thenCons(x, merge-with(xr, ys, cmp))
elseCons(y, merge-with(xs, yr, cmp))
// Sort a list using merge sort with a comparison function.// `cmp(a, b)` should return True when `a` should come before `b`.// Use `<=` for ascending order, `>=` for descending.funsort-by( xs :list<a>, cmp :(a, a)->bool ) :divlist<a>match xs
Nil -> NilCons(_, Nil) -> xs
_ ->
valmid = xs.length /2
merge-with(sort-by(xs.take(mid), cmp), sort-by(xs.drop(mid), cmp), cmp)
// Convenience: sort a list of ints in ascending order.funsort( xs :list<int> ) :divlist<int>
sort-by(xs, fn(a, b) a <= b)
Usage
funmain()
valnums = [3, 1, 4, 1, 5, 9, 2, 6]
println(nums.sort.show) // [1,1,2,3,4,5,6,9]
println(nums.sort-by(fn(a, b) a >= b).show) // [9,6,5,4,3,2,1,1]valfruits = ["banana", "apple", "cherry", "date"]
println(fruits.sort-by(fn(a, b) a <= b).show) // ["apple","banana","cherry","date"]
println(fruits.sort-by(fn(a :string, b :string) a.count < b.count).show) // ["date","apple","banana","cherry"]
In hica
hica exposes sort_by as a prelude function.
fun main(){println(sort_by([3,1,4,1,5],(a, b) => a <= b))
println(sort_by(["banana","apple","cherry"],(a, b) => a <= b))}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hej, while working on a merge sort for hica I did some light research but couldn't find any sorting functionality in Koka.
I wrote a version that I expose via codegen to hica.
The hica sort turned out okay, simple to use and does a top-down using list variant.
Maybe something the Koka community is interested to reuse?
Implementation
The sort is split into two functions:
merge-with— merges two already-sorted lists using a comparatorsort-by— recursive merge sort: split at midpoint, sort halves, mergeBoth require the
diveffect becausetake/dropmake the recursionnon-structurally decreasing.
Usage
In hica
hica exposes
sort_byas a prelude function.//C
Beta Was this translation helpful? Give feedback.
All reactions