0
votes

I'm new to scheme and am trying to write a quick sort in it and I can't seem to get the filter function figured out. I believe filter takes two arguments, a procedure that can be applied to each element in a list and then a list onto which the procedure is applied.

#lang racket

;Quick sort
(define(quickSort l)
    (cond
        [(null? l)('(()))]
        [(append(quickSort(filter(lambda(x)(< x car l))(cdr l))))(list(car l))(quickSort(filter(lambda(x)(>= x car l)(cdr l) cdr l)))]
        )
  )

(quickSort '(5 9 4 6 8 7 1 9))

This gives me a contract violation that says the (< x car l) should be ?real, and what it is getting is the car procedure.

I tried extracting the filter function and running it by itself, like so:

(filter(lambda(x)(< x car '(3 5 1 7 8))(cdr '(3 5 1 7 8))))

but this is telling me there is only one argument. I'm not sure why. It appears to me that there are two.

So, any ideas? I initially thought that it might be a problem with the arguments to the filter function, but now I'm thinking that maybe lambda can't accept the (car l) parameter? Would it not evaluate before being passed?

1

1 Answers

1
votes

Indent your code and then you'll see that there are syntactical errors. I have corrected them in the following:

;Quick sort
(define (quickSort l)
  (cond [(null? l) '()]
        [else (append (quickSort (filter (lambda (x) (< x (car l))) (cdr l))) 
                      (list (car l))
                      (quickSort (filter (lambda (x) (>= x (car l))) (cdr l))))]))