2017-09-18 HW05

posted Sep 18, 2017, 6:25 AM by Samuel Konstantinovich   [ updated Sep 18, 2017, 10:27 AM ]
Do Now:
Write a function 
(midPoint A B) that returns the value of the number halfway between numbers A and B. 
e.g.
(midPoint 1 2) -> 1.5
(midPoint -1 3) -> 1

Challenge Problem:

How might you be able to calculate the maximum of two numerical values values using only:

+ , - , *, / , abs


If you use the method on 5 and 12 the result of the expression should be 12.

If you use the method on 12 and 5 the result of the expression should be 12.

If you use the method on 5 and -12 the result of the expression should be 5.

If you use the method on -2 and 2 the result of the expression should be 2.


Classwork In groups of two:
-Draw a number line and sketch out what happens when you perform operations, this is more helpful than typing random things. Label your diagram with points A and B on the number line.
-Think about it in terms of math:
a. Is there a way to calculate the point halfway between A and B?
b. Is there a way to calculate the distance between A and B?
c. Does it matter if A or B is larger in the prior calculations? If it does, can you modify the calculation so that it doesn't matter?
d. Label the diagram with how you can calculate each portion, then write a formula for the max of A and B as a regular math formula.
e. NOW if you did all of that, you have figured out what you want to write as a scheme function (myMax A B) -> since max is already defined we are writing the function with the prefix that suggests you wrote it, and it is not the build in function.

Goal Booleans.

Depending on which scheme language you chose, True and False are written as:
#T   
#F
or even as:
#true
#false

You can generate booleans from numerical values using comparisons:
(< 3 3)
(<= 3 3)
(= 3 4)
(= 3 3.0)
(> 4 3)

You can use the built in functions: and,or,not  they operate on booleans.
(and #F #T)
(or #F #T)
(not (and #T #F))

You can write a function that returns a boolean as well:
(define (isGreaterThan10 x) (> x 10))
(isGreaterThan10 3)
(isGreaterThan10 13)
(isGreaterThan10 10)

LAB! Submit as HW05 as a comment on the homework server.
1. Define a function that returns true (evaluates to true) when the number is positive, false otherwise.
(this is just a simple test if you can write a boolean function. Normally you just write the boolean out.)
(define (isPositive x)
  (???))

2. Define a function that evaluates to true when a number is even, false otherwise. 
(define (isEven x)
  (???))

3. Define a function that evaluates to true when the parameter is a teen number. Teens: 13,14,15,16,17,18,19
(isTeen x)
(isTeen 12) -> #false
(isTeen 13) -> #true
(isTeen 114) ->#false

4. Define a function that evaluates to true when the sum of two numerical parameters is a teen, false otherwise. 
(isTeenSum x y)



Comments