Exercises
Exercise 3.1. [*] Take a look at this function definition:
Use the language
level Intermediate Student |
;; Number → Number
;; compute the after-tax weekly wage from the hours someone worked
;; pay-per-hour: $12.55
;; tax rate: 15%
;; social security: 5.5%
;; insurance cost: $100
(define (after-tax-income hours)
(- ; gross income:
(* hours 12.55)
; taxes:
(* (* hours 12.55) .15)
; social security:
(* (* hours 12.55) .055)
; insurance
100))
;; example/test:
(= (after-tax-income 40) 299.09)
Use local
to simplify the function body (“go slow!”).
Exercise 3.2. [*]
Developing the program strings-to-numbers*
in
exercise 1.2 required the definition of several
functions. It is good practice to reorganize programs such as those with
local
into a single function definition.
Exercise 3.3.
Develop the program numeric-matrix?
. The function consumes a
string matrix. It produces true
if all strings represent a
number; otherwise its return value is false
.
Here are the data definitions:
;; AMatrix
is one of: ;; ---empty
;; ---(cons LOS Matrix)
;; Constraint: In a matrix, the length of allLOS
is the same. ;; ALOS
is one of: ;; ---empty
;; ---(cons String LON)
Hint: The program requires several function definitions.
Constraint: Develop the program in two stages. First design all functions
at the top-level. Second organize them using local
so that there
is only one top-level function.
Exercise 3.4.
Design make-spread
. The function consumes two natural
numbers: n
and m
. Its result is a rectangular list:
rectangle
S-expression such as this:
(list (list (list 'td "cell00") (list 'td "cell01")) (list (list 'td "cell10") (list 'td "cell11")) (list (list 'td "cell20") (list 'td "cell21")) (list (list 'td "cell30") (list 'td "cell31")))
with 4
rows and 2
columns.
Hint: Use the function string-append
, which performs the obvious
operation on strings.
Use the function to develop the function make-table
, which
consumes two natural numbers and creates the following S-expression for
n = 3 and m = 2:
(list 'table (list 'tr (list 'td "cell00") (list 'td "cell01")) (list 'tr (list 'td "cell10") (list 'td "cell11")) (list 'tr (list 'td "cell20") (list 'td "cell21")))
Add the
teachpack servlet2.ss |
inform/html
(from the teachpack) to display the result of the
function in a browser.