Skip to content

Instantly share code, notes, and snippets.

@ebb
Last active August 29, 2015 14:27
Show Gist options
  • Save ebb/1b893858aee5fcc9678c to your computer and use it in GitHub Desktop.
Save ebb/1b893858aee5fcc9678c to your computer and use it in GitHub Desktop.
Puck language sample code.
\ INSERTION_SORT
\
\ Sort INPUT into ascending order as determined by the LESS_THAN binary
\ predicate. This sorting algorithm is stable.
Let insertion_sort less_than input.
(list.fold_left
Func sorted elem.
Loop left right.
(Init `nil sorted) \ Provides args for first iteration.
Match right
| `nil (list.reverse (elem:left))
| `cons.{elem' right'}
If (less_than elem elem')
(list.reverse_append left (elem:right))
(Recur (elem':left) right')
;
`nil
input)
\ Some tests.
(list.map (insertion_sort (<))
[List
[List] \ Empty list. Equivalent to `nil.
[List 1 2 3]
[List 2 2 2]
[List 3 2 1]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment