Why isn't this quicksort working?

This is a discussion on Why isn't this quicksort working? within the Scheme forums in Programming Languages category; frist why does this recurse endlessly? (define (++ . lists) (if (> (length lists) 0) (flatten (cons (car lists) (++ (cdr lists)))) '())) (define (quicksort lista) (if (= (length lista) 0) lista (let ((pivot (car lista))) (list (quicksort (filter (lambda (x)(< x pivot)) lista)) (list (pivot)) (quicksort (filter (lambda (x)(>= x pivot)) lista)))))) (quicksort '(22 3 23 12 3 4 2 12 22)) .. . procedure application: expected procedure, given: 2 (no arguments) >...

Go Back   Application Development Forum > Programming Languages > Scheme

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-21-2008, 06:07 PM
maestro
Guest
 
Default Why isn't this quicksort working?

frist why does this recurse endlessly?

(define (++ . lists)
(if (> (length lists) 0)
(flatten (cons (car lists) (++ (cdr lists))))
'()))


(define (quicksort lista)
(if (= (length lista) 0)
lista
(let ((pivot (car lista)))
(list (quicksort (filter (lambda (x)(< x pivot)) lista))
(list (pivot))
(quicksort (filter (lambda (x)(>= x pivot)) lista))))))


(quicksort '(22 3 23 12 3 4 2 12 22))
.. . procedure application: expected procedure, given: 2 (no arguments)
>

Reply With Quote
  #2  
Old 08-21-2008, 08:26 PM
Pascal J. Bourguignon
Guest
 
Default Re: Why isn't this quicksort working?

maestro <notnorwegian@yahoo.se> writes:

> frist why does this recurse endlessly?
>
> (define (++ . lists)
> (if (> (length lists) 0)
> (flatten (cons (car lists) (++ (cdr lists))))
> '()))


Because you always pass one argument to ++.


> (define (quicksort lista)
> (if (= (length lista) 0)
> lista
> (let ((pivot (car lista)))
> (list (quicksort (filter (lambda (x)(< x pivot)) lista))
> (list (pivot))
> (quicksort (filter (lambda (x)(>= x pivot)) lista))))))
>
>
> (quicksort '(22 3 23 12 3 4 2 12 22))
> . . procedure application: expected procedure, given: 2 (no arguments)


You are trying to call your pivot, and since it is 2, and not a
procedure, it cannot be called.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Un chat errant
se soulage
dans le jardin d'hiver
Shiki
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:24 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.