Racket Insertion Sort function that can sort in ascending or descending order

2.4k views Asked by At

I am attempting to write a DrRacket function that that can sort a list in ascending or descending order (by making the comparison operator a parameter). The sorting algorithm should be insertion sort. I called this second parameter cmp.

There are no warnings when I compile, but when I attempt to test the function; for example, (sort-list '(1 0 2 4) >), I receive this error:

sort-list: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
arguments...:

Here is my function as of now:

(define (sort-list l cmp)
  (define first-element (if (not (null? l)) (car l) 0))
  (cond ((null? l) (quote ()))
        (else (cons (find-shortest l first-element cmp) (sort-list (remove-member l (find-shortest l first-element cmp)))))))

(define find-shortest
    (lambda (tl b cmp)
      (cond ((null? tl) b)
            ((cmp (car tl) b) (set! b (car tl)) (find-shortest (cdr tl) b cmp))
            (else (find-shortest (cdr tl) b cmp)))))

(define remove-member
    (lambda (tl2 a)
      (cond ((null? tl2) (quote ()))
            ((= a (car tl2)) (cdr tl2))
            (else (cons (car tl2) (remove-member (cdr tl2) a))))))
0

There are 0 answers