Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Sublists of a list


; Precondition: lst is a proper list, n is an integer, n >= 0.
(define (front-sublist lst n)
  (cond ((= n 0) '())
        ((null? lst) (error "Too few elements in list"))
        ((not (null? lst))  
           (cons (car lst) (front-sublist (cdr lst) (- n 1))))))

; Precondition: The length of lst is a multiplum of n. n is an integer. n > 0.
(define (sublists lst n)
  (cond ((null? lst) '())
        (else (cons (front-sublist lst n) (sublists (list-tail lst n) n)))))