This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion Assorted Scheme Basics What do you think Scheme will print for the following? Assume this definition of the om procedure: (define (om foo) (word foo 'nom) > nom This will result in an error because nom has not been defined yet. If you wanted to print the word nom , you would type in 'nom . > (om 'nom) This will result in the value nomnom , where the variable foo in the body of the procedure om was replaced by the word nom . > (om nom) This will attempt to replace the variable foo with the value of the variable nom , resulting in an error, since nom was not defined. > ('om 'nom) This will attempt to call whatever is to the right of the first left parenthesis. However, since om is a word and not a procedure, this statement will fail. > (word 'a 'bc) This will result in the word abc . > (word 'a '(bc)) This will result in an error, because the arguments to the word procedure can only be words. > (sentence 'a 'bc) This will result in the sentence '(a bc) . > (sentence 'a '(bc d)) This will result in the sentence '(a bc d) . The arguments to the sentence procedure can be words or sentences. > (sentence '(a b) '(c d)) This will result in the sentence '(a b c d) . You're So Special: Special Forms 1. define : This would be a good time to recapitulate the two versions of define that we have seen. What is the difference between (define x 5) and (define (x) 5) ? The first call defines a variable named x and gives it a value of 5 . The second call defines a procedure named x with no arguments. This newly defined procedure will always return the value 5 . 2. if , cond : QUESTION: 1. Write the abs procedure, which implements the absolute value function. Use a cond statement. (define (abs x) (cond ((= x 0) 0) ((< x 0) (- x)) (else x))) 2. Convert the following cond statement into its equivalent if statement: (cond ((= area-code 415) (word 'san 'francisco)) CS61A Summer 2010 George Wang, Jon Kotker, Seshadri Mahalingam, Steven Tang, Eric Tzeng Notes developed based on those of Chung Wu and Justin Chen 1 ((= area-code 510) 'berkeley)...
View Full Document