{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

week1a_sol

# week1a_sol - CS61A Notes Week 1A Basics order of evaluation...

This preview shows pages 1–2. Sign up to view the full content.

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

This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern