{Record list_insertion:list_insertion_on_heap}

Where

Let (list_insertion_on_heap compare input)
    Let (insert sorted elem)
        Iterate {left right} From {[] sorted}
            Match right
            | `nil (LIST.reverse (elem :: left))
            | `cons.{elem' right'}
                Match (compare elem elem')
                | `less (LIST.reverse_append left (elem :: right))
                | _ Continue {(elem' :: left) right'}
                ;
            ;
    In
    (LIST.reduce input [] insert)

Let (list_insertion_on_stack compare input)
    For (LIST.reduce input [] insert)
        Define (insert sorted elem)
            Match sorted
            | `nil [elem]
            | `cons.{elem' sorted'}
                Match (compare elem elem')
                | `less (elem :: sorted)
                | _ (elem' :: (insert sorted' elem))
                ;
            ;

Where

Let LIST Package "list"