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?
(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?