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

Before the lab

Background


Time
10 minutes
Activity
Group discussion

Discussion of "before the lab"

Compare and Contrast

Time
10 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 x x))

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

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

Functions as values

Time
10 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.

Let's try and reduce the repeated code in this definition.

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

The following has a type error. How to fix it?

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

It turns out we can test our graphical functions, but it is a bit more work

(module+ test
  (require rackunit)
  (require racket/serialize)
  (check-equal? (serialize (series circle)) (series2 circle)))

Recursion

Time
20 minutes
Activity
Individual work
    #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))))

(list-length '(1 2 3))

Scribble

Time
20 min
Activity
Demo/Group Discussion

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

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

Modules

Time
30 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"))