Name: Anonymous 2012-10-21 4:05
Tell me, how bad/inefficient is my sorting loop? Just reading sicp. Language is scheme.
(define (sorter l)
(if (accumulate (lambda (a b) (and a b))
#t
(lambda (x) (if (null? (cdr x))
#t
(<= (car x) (cadr x))))
l
cdr)
l
(sorter (sort-into (car l) (cdr l)))))
(define (sort-into x l)
(define (helper x l)
(if (null? l)
()
(cons (min x (car l))
(helper (max x (car l))
(cdr l)))))
(append (helper x l) (list (maxlist (cons x l)))))
(define (maxlist l)
(accumulate max 0 car l cdr))
(define (accumulate combiner null-value term a next)
(cond ((null? a) null-value)
(else (combiner
(term a)
(accumulate combiner null-value term (next a) next)))))
(define (sorter l)
(if (accumulate (lambda (a b) (and a b))
#t
(lambda (x) (if (null? (cdr x))
#t
(<= (car x) (cadr x))))
l
cdr)
l
(sorter (sort-into (car l) (cdr l)))))
(define (sort-into x l)
(define (helper x l)
(if (null? l)
()
(cons (min x (car l))
(helper (max x (car l))
(cdr l)))))
(append (helper x l) (list (maxlist (cons x l)))))
(define (maxlist l)
(accumulate max 0 car l cdr))
(define (accumulate combiner null-value term a next)
(cond ((null? a) null-value)
(else (combiner
(term a)
(accumulate combiner null-value term (next a) next)))))