CS 115 – SOS Review Package – Midterm

And 5 3 2 1 1 true or statements indicate

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ) “uter”) (check-expect (front_part “compute”) “pute”) ;;swap_parts: string -> string ;;Purpose: To take a string, str, and swap the front and back parts ;;Examples: (swap_parts “computer”) -> “utercomp” ;; (swap_parts “compute”) -> “putecom” (define (swap_parts str) (string-append (back_part str) (front_part str))) ;;Tests: (check-expect (swap_parts “computer”) “utercomp”) (check-expect (swap_parts “compute”) “putecom”) BOOLEAN VALUES A Boolean value is either true or false. Boolean functions are functions that produce either true or false. Examples: (= x y) (< x y) (>= x y) Predicates: Ask questions with true or false answers (Ex. zero? number? integer? equal?) We can also build our own predicates: (define (long_name? name) (>= (string-length name) 8)) COMPLEX RELATIONSHIPS AND, OR, NOT • AND statements mean that both conditions must be true for the entire statement to be true o Ex. (and (= 5 (+ 3 2))(= 1 1)) -> true • OR statements indicate that only one of the conditions must be true o Ex. (or (= 5 5)(= 1 0)) -> true • NOT statements require the inner value to be false, for the NOT statement to be true o Ex. (not (= 5 6)) -> true Example 4 (Complex Relationship Trace): (define str “computer”) (and (<= 6 (string-length str))(equal? “mput” (substring str 2 6)) -> (and (<= 6 (string-length “computer”))(equal? “mput” (substring str 2 6)) -> (and (<= 6 8)(equal? “mput” (substring str 2 6))) -> (and true (equal? “mput” (substring str 2 6))) -> (and (equal? “mput” (substring str 2 6)) -> (and (equal? “mput” (substring “computer” 2 6)) -> (and (equal? “mput” “mput”)) -> (and true) -> (and) -> true CONDITIONAL RELATIONSHIPS Conditional expressions are basically a series of questions being asked and answers being produced if the answers to the questions are true. They allow you to analyze functions by “cases”. The general form is: (define (function parameter ...) (cond [(question 1) answer 1] [(question 2) answer 2] ... [else answer])) Example 5: Make a function called perfect_relationship, that consumes two strings, representing the names of two people, a positive integer, and a number representing how many hours the two people have been together. If they have known each other for more than 24 hours, they love each other. If they have known each other for less than 24 hours, but the positive integer is even, they still love each other. Otherwise, they hate each other. Make your function produce “name1 loves name2”. ;;perfect_relationship: string string int num[>=0] -> string ;;Purpose: create a string that represents the relationship status between two string, name1 and name2. I...
View Full Document

This note was uploaded on 01/19/2014 for the course CS 115 taught by Professor Troyvasiga during the Fall '13 term at University of Waterloo, Waterloo.

Ask a homework question - tutors are online