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)
Use structural (note that structural here is only indirectly related to Racket structs) recursion on natural numbers (not the simulated ones
from above, but regular Racket numbers like 1, 42, and 1337) to define a function (sum-factors n max-factor) that sums all
factors of n (including 1) no larger than max-factor
Recall the template for structural recursion on natural numbers:
(define (my-nat-fn n)
(cond
[(zero? n) ...]
[(positive? n) ... (my-nat-fn (sub1 n)) ...]))
Try to use only one recursive call to sum-factors like in the template.
Keep in mind that the n in the template might be a different
parameter of your function. You can use the builtin function remainder to test for divisibility.
The following tests should pass
;; 1+2+3 = 6
(check-expect (sum-factors 6 5) 6)
;; 1+2+4+7+14 = 28
(check-expect (sum-factors 28 27) 28)