UNB/ CS/ David Bremner/ teaching/ cs4613/ tutorials/ Recursion in racket

Background

Recursion

Corequisites
tests
    #lang racket
    (define (fact n)
      (cond
        [(zero? n) 1]
        [else                     ]))

    (module+ test
      (require rackunit)
      (check-equal? (fact 10) 3628800))
    #lang racket
    (define (list-length list)
      (if (empty? list)
          0
          (+ 1 (list-length list))))

    (module+ test
      (require rackunit)
      (define test-list '(1 2 3))

      (check-equal? (length test-list) (list-length test-list)))

The first / rest pattern

Prerequisites
quick

Section 2.3.2 of the Racket Guide gives several examples of recursively traversing a list, in particular

(define (my-map f lst)
  (cond
   [(empty? lst) empty]
   [else (cons (f (first lst))
               (my-map f (rest lst)))]))

This function uses a very important pattern, processing the first element of the list with first and recursively processing the remaining elements with rest. In this part of the lab you use this example as a starting point to rewrite the rainbow example from quick to use explicit recursion (i.e. calling first and rest), rather calling than map.