michael_michael


Joined 2 years ago
Homeworks submitted:
Homework comments:
1
0

About Me

No description provided.

Classes

How to Design Programs

Class status: Under Construction
Role: Creator
. 100% complete

Submitted Assignments

How to Design Programs: Lesson 1, HW 1

Brief summaries:

2.1 Numbers and Arithmetic Scheme permits the input of data as well as its manipulation through primitive operations (+ - * etc.). Scheme differentiates between exact numbers and inexact numbers. Inexact numbers may represent infinitely repeating decimals, for example.

2.2 Variables and Programs Scheme allows us to use placeholders to stand in for unknown quantities, or variables. A variable can consume an input and act on it.

2.3 Word Problems Programs often represent solutions to word problems. One important key to designing programs is effectively extracting relevant information from a word problem.

2.4 Errors Atomic expressions are composed of numbers and variables, whereas compound expressions may be composed of an operation, variables, and/or more expressions. Compound expressions are surrounded by parentheses. We can distinguish between three types of errors: Syntax errors (expressions that can't be evaluated because they are malformed), run-time errors (an expression that is faulty because of it is mathematically nonsensical, for example), and logical errors (the program or expression is well-formed, but it produces incorrect results).

2.5 Designing Programs Designing a program can be boiled down into a 'recipe' consisting of four phases: contract purpose and header, examples, body, and test

# Exercise 2.1.1.
# Find out whether DrScheme has operations for squaring a number#

'Yes, DrScheme has a sqr operation (sqr x)'

# For computing the sine of an angle# 

'Yes, (sin x)'

# And for determining the maximum of two numbers.

'Yes, (<>= x x) provide comparative operations.'

# Exercise 2.1.2.
# Evaluate (sqrt 4), (sqrt 2), and (sqrt -1) in DrScheme.
# 2, #i1.4142135623730951, 0+1i

# Then, find out whether DrScheme knows an operation for determining the tangent of an angle.

'Yes, (tan x)'

# Exercise 2.2.1.   Define the program Fahrenheit->Celsius,6 which consumes a temperature measured in Fahrenheit and produces the Celsius equivalent.

(define (fahrenheit->celsius f) (/ (- f 32) 1.8))

(define (dollar->euro d) (* d .74))

(define (triangle b h) (* (* b h) .5))

(define (convert a b c) (+ a (* b 10) (* c 100)))

(define (n^2+2 n) (+ (* n n) 2))

(define (.5*n^2+20 n) (+ 20 (* .5 (* n n))))

# Exercise 2.3.1.   Utopia's tax accountants always use programs that compute
# income taxes even though the tax rate is a solid, never-changing 15%. Define
# the program tax, which determines the tax on the gross pay.
# 
# Also define netpay. The program determines the net pay of an employee from the
# number of hours worked. Assume an hourly rate of $12

(define (tax pay) (* pay .85))

(define (netpay hours) (* 12 (tax hours)))

# Exercise 2.3.2.   The local supermarket needs a program that can compute the
# value of a bag of coins. Define the program sum-coins. It consumes four
# numbers: the number of pennies, nickels, dimes, and quarters in the bag; it
# produces the amount of money in the bag.


(define (coin-value p n d q) (+ p (* n 5) (* d 10) (* q 25)))

# Exercise 2.3.3.   An old-style movie theater has a simple profit function.
# Each customer pays $5 per ticket. Every performance costs the theater $20,
# plus $.50 per attendee. Develop the function total-profit. It consumes the
# number of attendees (of a show) and produces how much income the attendees
# produce.

(define (theater-profit n) (+ (* n 5) (* n -.5) -20))

# Exercise 2.4.2.   Enter the following sentences, one by one, into DrScheme's
# Definitions window and click Execute. Fix syntax errors:
# 
# (define (f 1) (+ x 10))
 
(define (f x) (+ x 10))

# (define (g x) + x 10)

(define (g x) (+ x 10))

# (define h(x) (+ x 10))

(define (h x) (+ x 10))

michael_michael 2 years ago