Last active
August 29, 2015 14:27
-
-
Save ebb/1b893858aee5fcc9678c to your computer and use it in GitHub Desktop.
Puck language sample code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ 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