;; CMPU101, Spring 2013 ;; Lecture #5 ;; Read pages 42-47 of posted readings for discussion of if and ;; cond special forms. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Making decisions--IF special form: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; One of the most critical operations done by computer programs ;; is decision making. For example, a program may need to test ;; if a number has a particular value or is within some range of ;; values. ;; ;; The most basic decision-maker is the IF special form. This ;; special form is best suited to the situation where there are ;; only 2 possible outcomes, #t or #f. ;; ;; The general shape of an IF special form is: ;; ;; (if questionExpression ;; questionTrueReturn ;; questionFalseReturn) ;; ;; The keyword IF is followed by three arguments, separated by ;; white space (that includes tabs, line breaks, etc). Naturally, ;; the entire expression is enclosed in parentheses as are all ;; special forms. ;; ;; The if special form is evaluated as follows: ;; ;; - The questionExpression is always evaluated. ;; ;; - If the questionExpression is true, the ;; questionTrueResult is evaluated and the ;; questionFalseResult is not evaluated. ;; ;; - Else, if the questionExpression is false, ;; the questionTrueResult is not evaluated and ;; the questionFalseResult is evaluated. ;; ;; - The questionTrueResult and questionFalseResult ;; are never both evaluated. ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Here is an example function that could use an if special form: ;; ;; Ex1: Define a function to return the inverse of its ;; input for non-zero integers or 0 if input is 0. ;; CONTRACT: ;; HEADER: ;; PURPOSE: ;; PRE-FUNCTION TESTS: ;; FUNCTION DEFINITION: ;(define inverse ; (λ (x) ; ...)) ;; In the event the result of the first expression is true, then ;; the second expression is evaluated (the true part); otherwise ;; the third expression is evaluated (the false part). Whichever ;; expression is evaluated, it is also the result (output) of the ;; entire if expression. ;;-------------- ;; Ex: Write a function called odd-or-even that consumes a natural number ;; and returns the string "odd" if the number is odd and "even" if the ;; number is even. ;;-------------- ;; Ex: Write a function called letter-grade that takes a natural number ;; less than or equal to 100 as input and produces the string ;; "A" if the number is greater than or equal to 85, "B" if the number ;; is less than 85 but greater than or equal to 72, "C" if the ;; number is less than 72 but greater than or equal to 60, "D" if the ;; number is less than 60 but greater than or equal to 50, and "F" if the ;; number is less than 50. Use "nested ifs". ;;-------------- ; ; One of the most common errors beginner programmers make is to use an if ; statement when only a single boolean expression is needed. ; ; Ex6: Write a function called may-drive? that consumes a number representing ; the age of a person and returning true if the age is >= 16 and false ; otherwise. ; Nested Conditionals: ; ; You can write one decision statement inside another, e.g., an if inside ; a cond clause, a cond inside a cond clause, an if inside an if, or a cond ; inside an if. These are called nested ifs and nested conds. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; The letter-grade function requires the "cascading" of if expressions. ;; Cascading ifs can be hard to read and understand. ;; ;; For decisions that have more than 2 possibilities, it is more ;; convenient to use another special form decision maker: COND ;; ;; The general shape of a COND special form is: ;; ;; (cond ;; [question1 answer1] ;; [question2 answer2] ;; ... ;; [else answerN]) ;; ;; where question1...questionN are predicates (inside parens) ;; that each evaluate to a boolean, and answer1...answerN evaluate ;; to the return value of the cond statement. The [question answer] ;; pairs are called CLAUSES. ;; ;; ELSE is a keyword that evaluates to #t and it can be replaced by ;; #t in the final clause. ;; ;; The cond statement evaluates in the following way: if question1 ;; is true, evaluate answer1; otherwise if question1 is false, skip ;; answer1 to evaluate question2. If question2 is true, evaluate ;; answer2; otherwise if question2 is false, skip answer2 to evaluate ;; question3... ;; ;; If none of the questions evaluates to true there is an error. That's ;; why the else is given at the end that is always evaluated when none ;; of the clauses that come before are evaluated. ;; ;; Only one answer is ever evaluated in a cond, just like the if. ;;---------------- ;; ;; Ex: Rewrite the letter-grade function written above so that ;; it uses a cond expression instead of a series of if state- ;; ments. ;; ;;---------------- ;; ;; Ex: Develop a function classify that tells what type of data its ;; input is by returning one of the strings "image", "string", ;; "number", "boolean", or "other", as appropriate.