10-01-2013 HWp

posted Oct 1, 2013, 5:22 AM by Samuel Konstantinovich   [ updated Oct 3, 2013, 5:17 AM ]
;Fox, Jamie
;MKS21 Period 0

Do Now. 
1. Write a function (xor a b) is true when a and b have different truth values, it is false when a and b are the same. 
Truth table for all possible combinations:
(xor #t #t) is #f
(xor #t #f) is #t
(xor #f #t) is #t
(xor #f #f) is #f

2. write a function that accepts an integer grade from 0-100.
;The function should evaluate to one of the following values:
;90-100  -> A
;80-89   -> B
;70-79   -> C
;65-69   -> D
;0-64    -> F





;We have 5 options and that makes the if statments get exceedingly long...


New Scheme Feature:  the cond statement

The format is:
(cond 
   ( boolean   expression)
   ( boolean2 expression2)
    ...
   (booleanN  expessionN)
)

Remember: 
A boolean other than #t and #f will have parenthesis like (< x 3) 
An expression may be a simple value such as 5, or something like (+ 2 (* x 3)) or even a boolean or function.
A real example is:
(cond
   (  (= x 1)  10)
   (  (> x 1) (* 2 x) )
   (  (< x 1) (/ x 2) )
)

Another nice thing is we can add a default case (like the last else of an if/else chain) The keyword else is equivalent to using a #t as your boolean, as it will always trigger.

(cond
   (  (= x 1)  10)
   (  (> x 1) (* 2 x) )
   (  else  0)
)


Now: Write a function (detectSign n) that returns 'positive 'negative or 'neutral , but use a cond statement!










Homework Cond: Rewrite Do Now question as a cond statement. Test it at home, bring in a printed copy tomorrow. 



Comments