All pastes #907487 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-02-17 17:25 UTC
rendered paste body
; Sorterar en lista rekursivt med mergesort
(defun mergesort_r(list)
  (if (endp list) '()
    (merger (mapcar #'list list))))

(defun merger(list)
  (if (endp (rest list))
    (first list)
   (merger (mergepairs list '()))))

; Hjälpfunktion till mergesort   
(defun mergepairs(list res)
  (if (endp (rest list))
      (append list res)
    (mergepairs 
     (rest (rest list)) 
     (cons (mergelists (first list) (second list) '()) res)
     '()))))

; Hjälpfunktion till mergesort
(defun mergelists(list1 list2 res)
  (cond
   ((endp list1)
    (append res list2))
   ((endp list2)
    (append res list1))
   ((< (first list1) (first list2))
    (mergelists (rest list1) list2 
     (append res (list (first list1)))))
   (t
    (mergelists list1 (rest list2) 
     (append res (list (first list2)))))))