CS3 Review Sheet: (Midterm 1) To create a procedure: (define (name arg1 arg2) (body)) - You can specify how many arguments your procedure can take. - the (body) part of this definition is where you write what your procedure does. CONSTRUCTORS: 1. word: - can take any number of arguments - can take anything besides sentences - glues all the given arguments together and returns it as one word - result has no spaces and no parenthesis EXAMPLES: (word 'a 'b 'c) ==> abc (word 'a "") ==> a * NOTE: "" is an empty word, aka a word with no characters 2. sentence: - can take any number of arguments - can take any type of argument - puts all arguments in a sentence with spaces in between - result is in a parenthesis with all the given arguments with spaces in between EXAMPLES: * se is the same as sentence, but is just shorter (se 'a 'b 'c) ==> (a b c) (se 'ab '(cd ef) '()) ==> (ab cd ef) (se '()) or (se ()) ==> () (se "" 'a ()) ==> ("" a) * NOTE: () or '() is an empty sentence, aka a sentence with no words SELECTORS: 1. first: takes the first item in a word or a sentence EX: (first 'abc) ==> a (first '(0 1 2 3)) ==> 0 Invalid arguments to first: (first "") and (first ()) ==> invalid argument to FIRST because "" () is empty. 2. butfirst (bf): takes out the first item and returns the rest of the argument EX: (bf 'abc) ==> bc (bf '(0 1 2 3)) ==> (1 2 3) (bf 'a) ==> "" (bf '(a)) ==> () Invalid arguments to bf: (bf "") and (bf ()) ==> invalid argument to BUTFIRST because "" and () is empty. 3. last: takes the last item in the argument and returns it. EX: (last 'abc) ==> c (last '(0 1 2 3)) ==> 3 Invalid arguments (last ()) and (last "") ==> invalid argument to LAST because "" and () is empty. 4. butlast (bl): returns the argument without the first item in the argument EX: (bl 'abc) ==> bc (bl '(0 1 2 3)) ==> (0 1 2) Invalid arguments (bl "") and (bl ()) ==> invalid argument to BUTLAST because "" and () is empty. SPECIAL PROCEDURES: 1. if: (if (predicate, returns #t or #f) (what return value you want if your predicate is true) (what return value you want if your predicate is false) ) - should always have a false return statement because if the predicate goes to the false statement and there's nothing there, it will just return 'okay'. 2. cond: (cond ((predicate1) (true value of predicate1)) ((predicate2) (true value of predicate2)) ((predicate3) (true value of predicate3)) ((predicate4) (true value of predicate4)) ((predicate5) (true value of predicate5)) ((predicate6) (true value of predicate6)) . . . ((predicate-n) (true value of predicate-n)) (else (return value if all the predicates are false) ) ) - cond can have any number of predicates or conditions - should have an else case because if there's no else condition and there's no predicates which returns true, then it will return 'okay'. BOOLEANS: #t or #f 1. equal?: takes two arguments of any kind and checks if the args are equal. If they are, returns #t, #f otherwise. 2. =: takes two numbers and checks if the numbers are equal. If they are, returns #t, #f otherwise. 3. member?: takes two arguments. first argument is a character or word second argument is a word or sentence checks if the character or word is a member of the second argument. 4. sentence?: takes one argument and checks if the argument is a sentence. If the given argument is a sentence, it returns #t, #f otherwise. 5. word?: takes one argument and checks if the argument is a word. If the given argument is a word, it returns #t, #f otherwise. 6. number?: takes one argument and checks if the argument is a number. If the given argument is a number, it returns #t, #f otherwise. 7. procedure?: takes one argument and checks if the argument is a procedure. If the given argument is a procedure, it returns #t, #f otherwise. 8. string?: takes one argument and checks if the argument is a string. If the given argument is a string, it returns #t, #f otherwise. 9. empty?: takes one argument and checks if the argument is empty. If the given argument is empty, it returns #t, #f otherwise. 10. even?: takes one argument and checks if the argument is even. If the given argument is even, it returns #t, #f otherwise. 11. odd?: takes one argument and checks if the argument is odd. If the given argument is odd, it returns #t, #f otherwise. *********** defining a boolean procedure: ************* (define (name? arg1) (if (predicate) #t #f)) SPECIAL FORMS: 1. and: - can take any number of arguments - evaluates its arguments one at a time from left to right. - stops evaluating when it finds a false result (you can think of it as finding the first false value or result) - if there are no false results, then it will return the last true result. EXAMPLES: (and (equal? 'a 'x) => #f ; and will evaluate only up to this point (= 1 1) => #t ; remains unevaluated, even if it is #t (number? -1) ) => #t ; remains unevaluated, even if it is #t ==> #f (and 1 2 3 4 5) ==> 5 because it returns the last true value that is evaluated. 2. or: - can take any number of arguments - evaluates its arguments one at a time from left to right. - stops evaluating when it finds a true result. (you can think of it as finding the first true value or result) - if there are no true values, then it will return #f. EXAMPLES: (or (= 1 2) (= 2 3) (equal? 1 2)) ==> #f (or 1 2 3 4 5) ==> 1 3. not: - takes one argument - If the argument's value is true, not returns #f, otherwise it returns #t. EXAMPLES: (not #t) ==> #f (not (equal? 1 3)) => (not #f) ==> #t QUOTE: - used when you don't want your argument to be evaluated - also used when passing characters, words, or sentences while calling a procedure in STK. PARENTHESIS: - used when you want to call a procedure - also used for representing a group of words as a sentence, but you will need a quote before the open paren e.g. '(your sentence) SOME USEFUL PROCEDURES: 1. appearances: - takes two arguments of any kind - returns the number of times the first argument appears in the second argument. 2. count: - takes one argument - returns the number of characters in the word or number of words in the sentence, depending on what kind of argument you give it. 3. item: - takes two arguments : first argument is a number, second argument is a word or sentence. - returns the (number)th character in the word or (number)th word in the sentence. ********** for other useful procedures, look in the back of SIMPLY SCHEME *********** ERRORS MESSAGES: 1. invalid argument to first/bf/last/bl 2. too few/many arguments 3. unbound variable 4. invalid argument to word 5. bad number in comparison 6. bad syntax 7. bad function