Skip to content

Instantly share code, notes, and snippets.

@narskidan
Created July 11, 2024 06:01
Show Gist options
  • Save narskidan/036641511f9afd2be9c755180a806de7 to your computer and use it in GitHub Desktop.
Save narskidan/036641511f9afd2be9c755180a806de7 to your computer and use it in GitHub Desktop.
Merge sort in Hoon
|%
++ merge
|= [a=(list @) b=(list @)]
^- (list @)
=| c=(list @)
|-
=/ empty-a =(0 (lent a))
=/ empty-b =(0 (lent b))
?: ?|(empty-a empty-b)
:(weld c a b)
=/ first-a (snag 0 a)
=/ first-b (snag 0 b)
?: (lth first-a first-b)
$(c (snoc c first-a), a (slag 1 a))
$(c (snoc c first-b), b (slag 1 b))
++ sort
|= l=(list @)
?: =(1 (lent l))
l
=/ middle (div (lent l) 2)
=/ left $(l (scag middle l))
=/ right $(l (slag middle l))
(merge left right)
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment