Before the lab
Assignments
- Review assignment 1. Bring any questions to the lab.
Reading
Read FICS unit 3
Read FICS unit 4
Read FICS unit 3
Read FICS unit 4
labs/L03 inside your ~/cs2613 git repositoryand and orAs you read in FICS unit
3,
we can understand evaluation ("running") of Racket programs as a
sequence of "reductions" or "substitutions". These rules are similar to the reduction steps in the DrRacket stepper.
The stepper uses the following rules for and and or
(notice that these rules enforce short circuit evaluation)
(and true exp2 ...) => (and exp2 ...)
(and false exp2 ...) => false
(or true exp2 ...) => true
(or false exp2 ...) => (or exp2 ...)
(and) => true
(or) => false
Following Exercise 7, write a new set of rules
that requires at least two arguments for and and or.
The rules are for human consumption; you can write them
as comments in DrRacket. You can write "exp1 exp2 ..." to mean at least 2 expressions.
Discuss your answers with a your group, and try a couple evaluation small examples by hand using your rules.
Write the times function from
Exercise 11 in FICS.
You can (and should) use the following code from the linked discussion
#lang htdp/bsl
(define-struct Z ())
(define-struct S (pred))
(define (pred nat)
(cond
[(Z? nat) (error "can't apply pred to Z")]
[(S? nat) (S-pred nat)]))
(define (plus nat1 nat2)
(cond
[(Z? nat1) nat2]
[(S? nat1) (make-S (plus (S-pred nat1) nat2))]))
Here is the template for structural recursion on (simulated) natural numbers. See the linked text (or the plus function just above) for how to add a second "carried-along" parameter.
(define (my-nat-fn nat)
(cond
[(Z? nat) ...]
[(S? nat) ... (my-nat-fn (S-pred nat)) ...]))
Here are some tests to get you started. As always, try to have as
complete test coverage as possible. Depending on how you choose the
language in
DrRacket,
the line (define-struct S (pred)) may show partial coverage; you can
ignore this for this lab.
;; 0 * 0 = 0
(check-expect (times (make-Z) (make-Z)) (make-Z))
;; 0 * 1 = 0
(check-expect (times (make-Z) (make-S (make-Z))) (make-Z))
;; 2 * 1 = 2
(check-expect (times (make-S (make-S (make-Z)))
(make-S (make-Z)))
(make-S (make-S (make-Z))))
You may find it helpful to refer back to your solution from L02.
Write the compare function from
Exercise 11 in FICS. Your function compare
should use the struct definitions Z and S, and pass the following tests
;; 0 = 0
(check-expect (compare (make-Z) (make-Z)) 'equal)
;; 0 < 1
(check-expect (compare (make-Z) (make-S (make-Z))) 'less)
;; 1 > 0
(check-expect (compare (make-S (make-Z)) (make-Z)) 'greater)
;; 2 > 1
(check-expect (compare (make-S (make-S (make-Z)))
(make-S (make-Z))) 'greater)
See if you can come up with answers to the following questions for next time.
The programming languages we will study this term are mainly dynamically typed. This means that not only the value but also the type of variables can change at runtime. Why does this make testing even more important?
What kind of software problems is testing not well suited to find?
Why might mutable state (e.g. instance variables in Java) make writing unit tests harder?