title: Scheme quicksort - Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)
body: <p>I just started learning Scheme (Petite Chez Scheme) and as a challenge to myself I'm trying to implement quicksort. However I get the following exception when I run it:</p>
  
  <pre><code>Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)
  </code></pre>
  
  <p>Here's my Scheme session from Emacs:</p>
  
  <pre><code>Petite Chez Scheme Version 8.4
  Copyright (c) 1985-2011 Cadence Research Systems
  
  &gt; (define (set a i k)
      (define (reduce-list a i n)
        (if(= i n)
       a
       (reduce-list (cdr a) (+ i 1) n)))
      (if(= i 0)
         (cons k (cdr a))
         (let ((b (cons k (reduce-list a 0 (+ i 1)))))
       (let push-front ((lst b) (original-list a) (n (- i 1)))
         (if(&lt;= n 0)
            (cons (list-ref original-list 0) lst)
            (push-front (cons (list-ref original-list n) lst) original-list (- n 1)))))))
  
  (define (swap lst i j)
      (let ((a (list-ref lst i))
        (b (list-ref lst j)))
        (set (set lst i b) j a)))
  
  (define (partition a i j r)
      (cond [(= j r) (cons (+ i 1) (swap a (+ i 1) j))]
        [(&lt;= (list-ref a j) (list-ref a r)) (partition (swap a j (+ i 1)) (+ i 1) (+ j 1) r)]
        [else (partition a i (+ j 1) r)]))
  
  (define (quicksort a p r)
      (if(&lt; p r)
         (begin(
            (let* ((c (partition a (- p 1) p r))
              (q (car c))
              (b (quicksort (cdr c) p (- q 1))))
          (quicksort b (+ q 1) r))))
         a))
  
  &gt; (define lst (list 1 9 2 8 3 7 4 6 5))
  &gt; (define n (length lst))
  &gt; (trace quicksort)
  (quicksort)
  &gt; (quicksort lst 0 (- n 1))
  |(quicksort (1 9 2 8 3 7 4 6 5) 0 8)
  | (quicksort (1 2 3 4 5 7 8 6 9) 0 3)
  | |(quicksort (1 2 3 4 5 7 8 6 9) 0 2)
  | | (quicksort (1 2 3 4 5 7 8 6 9) 0 1)
  | | |(quicksort (1 2 3 4 5 7 8 6 9) 0 0)
  | | |(1 2 3 4 5 7 8 6 9)
  | | |(quicksort (1 2 3 4 5 7 8 6 9) 2 1)
  | | |(1 2 3 4 5 7 8 6 9)
  Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)
  &gt; 
  </code></pre>
  
  <p>Can anyone tell me what's going wrong? Thank you in advance</p>