# Spring 2005

## Lecture #1: The Best Little Pizza House in Texas

1. Signing up for lab sessions

Submit 3 choices of lab sessions in order of preference.  Check the Comp 212 web page (www.owlnet.rice.edu/~comp212/) at around 2 PM for the lab roster.  The lab roster will also be posted outside of DH 3098.

 Lab Session Monday Tuesday 3:30-5:00 PM M1 7:00-8:30 PM M2 T1

2. Pizza Mania

Pizza Mania is having a special on the same kind of pizza (i.e. same thickness and same ingredients). The special comes in two shapes: rectangular (4" by 6") for \$4.99 and round (5" in diameter) for \$3.99. Which one is the better deal? Write a program to solve this problem. Why do we want to write a program to solve something as simple as this one anyway?

### A Few Functional Programming (FP) Solutions

Solution #1: We can solve this problem by writing the following Scheme function using the program design methodology taught in Comp 210.

; Data Definition:
; the price of a pizza is a number
; the width and the length of a rectangular pizza are numbers
; the diameter of round pizza is a number.

; Contract:
; is-better: number number number number number -> boolean
; Purpose:
; (is-better p1 x y p2 d) returns true
; if the rectangular pizza with price p1, width x, and length y
; is a better deal than the round pizza with price p2 and diameter d,
; else returns false.
; Examples:
; (is-better 4.99 4 6 3.99 5) = true.
; (is-better 4.99 4 6 3.99 4) = false.
; (define (is-better p1 x y p2 d) ...)

(define (is-better p1 x y p2 d)
; compare the price/area ratios of the rectangular and round pizzas
)

What is good about the above solution?

What if the special is about two round pizzas of different diameters instead?

What is the real problem to be solved here?  Does the above solution solve the real problem or a specific example of the problem?

!!!More discussion here!!!!

Solution #2: Here is a more flexible solution in Scheme.

;Data Definition:
; A Pizza is either
;   (make-RectPizza p w l) where
; p is the price of the rectangular pizza with width w and length l,
; or
;   (make-RoundPizza p d) where
; p is the price of the round pizza with diameter d.

; Contract:
; is-better1: Pizza Pizza -> boolean
; Purpose:
; (is-better1 piz1 piz2) returns true
; if the pizza piz1 is a better deal than the pizza piz2
; else returns false.
; Examples:
; (is-better1 (make-RectPizza 4.99 4 6) (make-RoundPizza 3.99 5)) = true.
; (is-better1 (make-RectPizza 4.99 4 6) (make-RoundPizza 3.99 4)) = false.
; (define (is-better1 piz1 piz2)
;     (cond
;        [(RectPizza? piz1)...]
;        [(RoundPizza? piz1) ...]))

(define (is-better1 piz1 piz2)
; compare the price/area ratios of pizzas piz1 and piz2
)

What is good about the above solution?

What if the special is about a rectangular pizza and a triangular pizza instead?

What is the real problem to be solved here?  Does the above solution solve the real problem or a specific example of the problem?

!!!More discussion here!!!!

Solution #3: An even more flexible solution to the problem is to write a function to compute the price/area ratio of a pizza and use it to find the better deal.

; Contract:
; price/area: Pizza -> number
; Purpose:
; (price/area piz) returns the price/area ratio of the pizza piz
; Examples:
; (price/area (make-RectPizza 4.99 4 6)) = ???.
; (price/area (make-RoundPizza 3.99 4)) = ???.
; (define (price/area piz)
;     (cond
;        [(RectPizza? piz)...]
;        [(RoundPizza? piz) ...]))

(define (price/area piz)
; computes the price/area ratio of the pizza piz.
)

; Contract:
; is-better2: Pizza Pizza -> boolean
; Purpose:
; (is-better2 piz1 piz2) returns true
; if the pizza piz1 is a better deal than the pizza piz2
; else returns false.
; Examples:
; (is-better2 (make-RectPizza 4.99 4 6) (make-RoundPizza 3.99 5)) = true.
; (is-better2 (make-RectPizza 4.99 4 6) (make-RoundPizza 3.99 4)) = false.
; (define (is-better2 piz1 piz2)
;   (... (price/area piz1) ...
;   ... (price/area piz2) ...)

(define (is-better2 piz1 piz2)
(< (price/area piz1) (price/area piz2)))

What is good about the above solution?

What if the special is about a rectangular pizza and a triangular pizza instead?

What is the real problem to be solved here?  Does the above solution solve the real problem or a specific example of the problem?

!!!More discussion here!!!!

### An Object-Oriented Programming (OOP) solution...

To be continued...  Same time, same channel...

dxnguyen  at rice edu