;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname conditionals) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (define a 5) (define b 7) (define c 5) (< a b) (< a c) (<= a c) (= a c) (= a b) (or (< a b) (< b a)) (and (< a b) (< a c)) (not (< a b)) ; lazy evaluation: if the value is known from the first ; argument of "and" or "or", the second one is not evaluated ; In the examples below (/ 5 0) never gets evaluated because ; it is not needed for computing the value of the expression (and (> a b) (= 1 (/ 5 0))) (or (< a b) (= 1 (/ 5 0))) ;; Contract: number -> boolean ;; Purpose: to determine if the parameter is greater or equal ;; to 21 (define (is-over-21 age) (>= age 21)) (is-over-21 18) (is-over-21 25) (is-over-21 21) ;; Is this correct? (define (is-under-21 age) (not (> age 21))) ;; write a function that returns true if the parameter x ;; is satisfies the inequality 1 < x <= 3 ;; and false otherwise ;; write a function that returns true if the parameter x ;; is less than -1 or greater than 1 ;; and false otherwise ;; conditionals ("if" statements) (cond [(< a 0) (- a)] ; if a < 0, return -a [(>= a 0) a]) ; if a >= 0, return a (define d -5) (cond [(< d 0) (- d)] [(>= d 0) d]) ;; using "else": (cond [(< d 0) (- d)] [else d]) ;; make this conditional into a function so that ;; we can use it for multiple numbers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Contract: number -> number ;; Purpose: find the absolute value of the parameter x ;; Examples: (abs-val 5) gives 5, (abs-val -5) gives 5, ;; (abs-val 0) gives 0 (define (abs-val x) (cond [(< x 0) (- x)] [else x])) ;; Tests (abs-val 5) ;; expected value 5 (abs-val -5) ;; expected value 5 (abs-val 0) ;; expected value 0 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; more choices (cond [(< a 0) -1] [(> a 0) 1] [(= a 0) 0] ) ;; same condition as a function: ;; Contract: number -> number ;; Purpose: computes the sign of x as follows: ;; if x < 0 then the sign is -1, if x > 0, the sign is 1, ;; otherwise the sign is 0 ;; Examples: (sign -10) is -1, (sign 10) is 1, (sign 0) is 0 (define (sign x) (cond [(< x 0) -1] [(> x 0) 1] [(= x 0) 0] )) ;; Tests: (sign -10) ;; expected value -1 (sign 10) ;; expected value 1 (sign 0) ;; expected value 0