PDA

View Full Version : Problem with a Scheme function


GnuVince
02-22-2004, 08:34 PM
;; ROTATE
(define (all? elt lst)
(if (null? lst)
#t
(if (equal? elt (car lst))
(all? elt (cdr lst))
#f)))


(define (cars lst)
(if (all? '() lst)
'()
(cons (caar lst) (cars (cdr lst)))))

(define (cdrs lst)
(if (all? '() lst)
'()
(cons (cdar lst) (cdrs (cdr lst)))))

(define (rotate lst)
(if (null? lst)
'()
(cons (reverse (cars lst)) (rotate (cdrs lst)))))


This code is used to rotate a square list in Scheme. It rotates, but it also adds an empty list to the rotated list, which bugs me greatly:


> l
((1 2 3) (4 5 6) (7 8 9))
> (rotate l)
((7 4 1) (8 5 2) (9 6 3) ())
>


I can't figure where this empty list is coming from. Anyone can help?

GnuVince
02-22-2004, 09:23 PM
Got it, never mind. I changed (null? lst) to (all? '() lst) and now it works. Sorry for the wasted disk space.

Strike
02-23-2004, 04:41 AM
Ah, but by the CF owners' forum logic, you've increased value (otherwise, why would they use post count in a time quantum as their metric for who is in their little contest?)! Actually, you have, but I haven't (not with this post anyway), and yet it still counts as beneficial. In your face, system!