CSC 208-01 (Fall 2023)

Lab: Project 1

Problem 1: Boolean Blasters

Consider the following top-level Racket definitions over booleans that give implementations of the standard boolean operations not, and, and or:

(define my-not
  (lambda (b)
    (if b #f #t)))

(define my-and
  (lambda (b1 b2)
    (if b1 b2 #f)))

(define my-or
  (lambda (b1 b2)
    (if b1 #t b2)))

Prove the following claims of program equivalence about these definitions:

Claim 1: (my-not (my-and #t #f))#t.

Claim 2: There exists a boolean b1 such that for all booleans b2, (my-or b1 b2)#t.

Claim 3 (Negation is an Involution): For any boolean b, (my-not (my-not b))b.

Claim 4 (De Morgan’s Law): For any pair of booleans b1 and b2, (my-not (my-and b1 b2))(my-or (not b1) (not b2)).

For any derivations that you write, you may evaluate a function call directly to the branch of the selected conditional in its body.

Problem 2: The Corporate World

The CEO of a company has asked their in-house programming team to build a new payment system. Obviously, this payment system is written in Racket. The code for the system is given below.

#lang racket

(require racket/match)

;;; An employee in this payment system is a list of four elements:
;;; + The employee's position, a string,
;;; + The employee's name, a string
;;; + The employee's time at the company in years, a nonnegative integer, and
;;; + Whether the employee wears shoes in the office, a boolean.

;;; (senior? employee) returns #t iff the employee is senior.
(define senior?
  (lambda (employee)
    (match employee
      [(list "Accountant" _ yrs _) (>= yrs 5)]
      [(list "Programmer" _ yrs _) (>= yrs 5)]
      [(list "Manager" _ yrs _)    (>= yrs 8)]
      [(list "CEO" _ yrs _)        (>= yrs 1)])))

(define acct-base 2)
(define prog-base 5)

;;; (pay employee) returns the pay of the given employee
;;; in tens of thousands of dollars, an integer.
(define pay
  (lambda (employee)
    (match employee
      [(list "Accountant" _ _ _)
       (if (senior? employee) (+ 2 acct-base) acct-base)]
      [(list "Programmer" _ _ shoes?)
       (if shoes? (* prog-base 8) (* prog-base 4))]
      [(list "Manager" name yrs shoes?)
       (cond [(equal? name "Tony")     -1]
             [(< yrs 2)                prog-base]
             [(not (senior? employee)) (* prog-base 2)]
             [else                     (* prog-base 10)])]
      [(list "CEO" _ _ _) 1])))

Problem 3: Zero Is The Hero

  1. Implement a Racket function dropzeroes that takes a list l of numbers as input and returns l but with all 0s. For example (dropzeroes '(1 3 0 1 0 2)) --> '(1 3 1 2). Make sure to test your code in Dr. Racket and add your code to this write-up using a code block.

  2. Prove the following property of dropzeroes:

    Claim (Idempotency of dropzeroes): (dropzeroes (dropzeroes l))(dropzeroes l).

    You may take the shortcut of evaluating a recursive function call directly to the branch that it selects in a single step.

    (Hint: be precise with your program derivations and every step you make! In particular, make sure that every step is justified and you consider all possibilities of evaluation.)