Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:59:07+00:00 2026-06-01T17:59:07+00:00

I just started learning Scheme (Petite Chez Scheme) and as a challenge to myself

  • 0

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:

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

Here’s 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 anyone tell me what’s going wrong? Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:59:09+00:00Added an answer on June 1, 2026 at 5:59 pm

    The problem is with the

    begin
    

    in quicksort. When

    (quicksort b (+ q 1) r)
    

    eventually returns a (which is actually b in the parent quicksort), then the let* block reduces 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 fails with an error. If you remove the begin, the let block will behave as you intend.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
I just started learning Common Lisp a few days ago, and I'm trying to
I just started learning Flex this week, and I cannot get an image path
Just started learning Rails (3). I am tearing my hair out trying to find
Just started learning Scheme. I'm using Dr. Racket as my compiler/interpreter. I need some
I just started learning Zend. I managed to get the basic working (using zf
I've just started learning Functional Programming (Scheme). But I still have problems thinking functionally.
Just started learning Ruby metaprogramming. Looking at Object.methods I get: Object.methods => [ :allocate,
I just started learning SQL a couple of days ago, and I'm trying to
I'm just started learning scheme and can't quite understand why this function does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.