{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS 115 – SOS Review Package – Midterm

Define our own structures 3 each has constructor

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: f atime, is greater than 24, the function produces "name1 loves name2", if its less than or equal to 24, and int is even, then "name1 loves name2". otherwise, we get "name1 hates name2" ;;Examples: (perfect_relationship "miss universe" "Dylan" 5 42) => "miss universe loves Dylan" ;;(perfect_relationship "Dylan" “Scheme" 5 23) => "Dylan hates Scheme" (define (perfect_relationship name1 name2 int atime) (cond [(> atime 24)(string-append name1 " loves " name2)] [(even? int)(string-append name1 " loves " name2)] [else (string-append name1 " hates " name2)])) ;;Tests (check-expect(perfect_relationship "Dylan" "Scheme" 5 23)"Dylan hates Scheme") (check-expect(perfect_relationship "miss universe" "Dylan" 5 42)"miss universe loves Dylan") SYMBOL A symbol starts with the quote ‘ followed by something (ex. ‘blue, ‘red). They are best used for comparisons because it is more efficient than comparing strings. A symbol has a value like the number 6. Type “symbol” in the contract. CHARACTER Ex. #\space, #\1, #\a Built ­in Predicates: • char ­upper ­case? • char ­lower ­case? • char ­numeric? • char ­whitespace? Type “char” in the contract. INTRO TO STRUCTURES: POSN STRUCTURES A structure is a way of bundling data together to form a single package. 1. You can create functions that consume and/or produce structures 2. Define our own structures 3. Each has • Constructor function • Selector function • Type Predicate Function Example 6: A posn is a structure that has two fields containing numbers that represent x and y coordinates. Constructor function: (make-posn num1 num2) Selector functions: (posn-x (make-posn num1 num2)) -> num1 (posn-y (make-posn num1 num2)) -> num2 Type Predicate: (posn? (make-posn num1 num2)) -> true Ex. (define point (make-posn 20 45)) (posn-x point) -> 20 (posn-y point) -> 45 (posn? point) -> true DATA DEFINITIONS AND STRUCTURE DEFINITIONS Data Definition: a comment specifying the types of data being used Ex. ;;a posn is a structure (make-posn xcoord ycoord), where ;;xcoord is a number ;;ycoord is a number Structure Definition: a code defining the structure (like we did above) and allowing us to then use constructor, selector and predicate functions for the definition (define-struct sname (field1 field2 ...)) Doing this gives 3 functions: 1. Constructor: make-sname 2. Selectors: sname-field1, sname-field2 3. Type Predicate: sname? Example 7: Write a function called reply that consumes an email and a string and produces an email that is a reply to the email consumed using the message given. The contact that sends the reply email wil...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online