Lecture 3, June 23

The lecture notes I post serve two purposes: to remind you of the topics we discussed and to provide any interesting code examples I did during lecture. I would describe these notes as an outline, not a summary, of what I talked about during lecture. You are not expected to be able to learn the material simply from examining these notes, nor is reading these notes a reasonable substitute for attending lecture.

Program Design

The instructions from chapter 3 in the text require some modification for typed racket. For every function you write for the rest of the class you will do the following:

  1. Type declaration. Decide on the types that you will use to represent the data and write the function type. See the typed racket notes on the course website for help on how to write types.
  2. Purpose. Write a short description of what the function does.
  3. Examples. You are not required to include these in the work you turn in: they are redundant with the tests. However, this can be an important step in understanding the function when you are working on a complicated project.
  4. Definition. Write out the definition of the function. This is the part you will spend most of your time on.
  5. Tests. Write out tests using check-expect, check-within, and check-range. You must include (require typed/test-engine/racket-tests) at the top of your file and (test) at the bottom.

Scope

Variables may be defined with global scope or with function scope. Variables created with define statements are global and can be accessed anywhere, including inside functions. Variables that indicate the inputs to functions have function scope and are only accessible from inside the function body.

Conditionals and if statements

You should be familiar with if and cond from the reading. They are extremely important and you should review them if you find them confusing. The book does an excellent job describing them and providing examples and exercises. You will get lost very quickly if you cannot follow what's happening with them.

Recursion

We began working on the factorial function. We will continue discussing it on Monday.
(: factorial (Nonnegative-Integer -> Positive-Integer))
; Computes the factorial function
(define (factorial n)
  (cond
    [(= n 0) 1]
    [else (* n (factorial (- n 1)))]))

(check-expect (factorial 0) 1)
(check-expect (factorial 3) 6)
(check-expect (factorial 6) 720)
(check-expect (factorial 7) 5040)

Miscellaneous other

The following is a copy of the code examples I created during lecture.
#lang typed/racket

(require typed/test-engine/racket-tests)
(require typed/2htdp/image)

(: n : Number)
(: n Number)
(define n 3)

(: f : (-> Number Number))
(: f (-> Number Number))
(: f : (Number -> Number))
(: f (Number -> Number))
(: f : Number -> Number)
; The function f takes one number as input and computes x^2 + 3
; Example: (f 2) -> 7
; Example: (f 3) -> 12
; Example: (f -1.2) -> 4.44
(define (f n)
  (+ 3 (* n n)))

(check-expect (f 2) 7)
(check-expect (f 3) 12)
(check-expect (f -12/10) 444/100)
(check-within (f -1.2) 4.44 0.0000001)

(check-expect (* 2 2) 4)
(check-expect (+ 4 8) 12)
(check-expect (f 2) 7)

(check-within (f 2) 7 .00000001)
(check-within (* (sqrt pi) (sqrt pi)) pi 0.00000000001)

(check-range (* (sqrt pi) (sqrt pi)) 3.13 3.15)

(check-expect (string-append "Hi" "Hello") "HiHello")
(check-expect (< 0 1) true)

(: number->square (Positive-Integer -> Image))
; Produces a red square with a given side length
(define (number->square side-len)
  (square side-len "solid" "red"))
"Eyeball test: (number->square 30)"
(number->square 30)
"Eyeball test: (number->square 50)"
(number->square 50)
"Eyeball test: (number->square 60)"
(number->square 60)

(: h (Real -> Real))
; This function computes (x + 2) if x is less than -1
; The absolute value of x if x is between -1 and 1
; Otherwise this function computes x^2
(define (h x)
  (cond
    [(< x -1) (+ x 2)]
    [(<= (abs x) 1) (abs x)]
    [else (* x x)]))
(check-expect (h 0) 0)
(check-within (h .5) .5 .000000001)
(check-within (h -.75) .75 .000000001)
(check-expect (h -1) 1)
(check-expect (h -3) -1)
(check-expect (h 3) 9)

(: factorial (Nonnegative-Integer -> Positive-Integer))
; Computes the factorial function
(define (factorial n)
  (cond
    [(= n 0) 1]
    [else (* n (factorial (- n 1)))]))


(test)