Quicksort scheme - Exception: attempt to apply a non-procedure (1 2 3 4 5 7 ...)

I just started learning Scheme (Petite Chez Scheme) and as a challenge to myself, I am trying to implement quicksort. However, when I start, I get the following exception:

Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)

Here is my Scheme session from Emacs:

Petite Chez Scheme Version 8.4
Copyright (c) 1985-2011 Cadence Research Systems

> (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(<= 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))]
      [(<= (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(< 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))

> (define lst (list 1 9 2 8 3 7 4 6 5))
> (define n (length lst))
> (trace quicksort)
(quicksort)
> (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 ...)
> 

Can someone tell me what is going wrong? Thank you in advance

+1
source share
3 answers

A problem with

begin

in quick sort. When

(quicksort b (+ q 1) r)

eventually returns a (which is actually b in the parent quicksort), then let * block decreases from

(define (quicksort a p r)
  (if(< 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))

to

(define (quicksort a p r)
  (if(< p r)
     (begin
        (b)) ;this is the cause of the error 
     a))

And since b is a list, trying to call it with an error. If you remove the beginning, the let block will behave as you intend.

+1

quicksort (begin( ) (quicksort b (+ q 1) r).

( begin ) .

, let*, let *. , , - , .

, .

(let ([a 1] [b 2] [c 3])
  (set! b 3)
  a
  b)

3 ( )

:

(let ([a 1] [b 2] [c 3])
  (begin
     (set! b 3)
     a
     b))
0

, -, , , , / (begin ()).

(begin ()), (if ()), .

, (begin ()) , " ", .

:

(do ([i 0 (+ i 1)]) [(< i 10)]
  (if (= (% i 2) 1)
    (printf 'odd)
    (begin
      (printf 'even)
      'even)))

(begin ()), , Chez , i . i , begin, .

, Chez Scheme . " " :

" begin [...] , let -."

0
source

Source: https://habr.com/ru/post/1665097/


All Articles