UNB/ CS/ David Bremner/ teaching/ cs2613/ labs/ CS2613 Lab 4

Background

Part 1 Compare and Contrast

Time
15 minutes
Activity
Group discussion

For each of the following racket samples, what is the closest concept from Java, or roughly how would you do the same thing in Java? How are the two similar? How are they different? Try to classify differences as deep or shallow (completely subjective!)

  1. #lang racket, #lang slideshow

  2. (+ 1 2)

  3. (rectangle 10 10)

  4. (define r 10)

  5. (define (square x) (rectangle 10 10))

  6. (colorize (square 10) "red")

  7. (let* ([x 10] [y (+ x 10)]) (* x y))

Part 2 Functions as values

Time
15 minutes
Activity
Demo

Most of you probably read Part 6 of the Quick Tutorial because the function series defined there was used in part 7.

(define (series mk)
  (hc-append 4 (mk 5) (mk 10) (mk 20)))

Initial attempt fails. What to add?

(define (series2 mk)
  (hc-append 4 (map mk '(5 10 20))))

This really starts to pay off with more repetition

(define (series3 mk)
  (apply hc-append 1 (build-list 100 mk)))

Part 3 Recursion

Time
20 minutes
Activity
Small groups
    #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)))

Part 4 Scribble

Time
25 min
Activity
Demo/Group Discussion

Scribble is one of the markup languages supported by frog. It has the advantage of being easily extendible using Racket.

    @(define (todo hdr . lst) (list (bold hdr) (apply itemlist (map item lst))))

Part 5 Modules

Time
25 minutes
Activity
Individual

In Lab 2 we covered the Quick tutorial section on modules We've also been using modules as part of unit testing. In this section of the lab we we will further explore modules, submodules, require and provide.

    #lang racket
    (define (hello) (displayln "Hello world!"))
    (check-equal? (with-output-to-string hello) "Hello world!\n")
    (check-equal? (with-output-to-string hello) (begin (sleep 3) "Hello world!\n"))