notes02 - (member letter ’(a e i o u Pigl introduces...

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

View Full Document Right Arrow Icon
Examples: (define (plural wd) (word wd ’s)) This simple plural works for lots of words (book, computer, elephant) but not for words that end in y (Fy, spy). So we improve it: ;;;;; In file cs61a/lectures/1.1/plural.scm (define (plural wd) (if (equal? (last wd) ’y) (word (bl wd) ’ies) (word wd ’s))) If is a special form that only evaluates one of the alternatives. Pig Latin: Move initial consonants to the end of the word and append “ay”; SCHEME becomes EMESCHAY. ;;;;; In file cs61a/lectures/1.1/pigl.scm (define (pigl wd) (if (pl-done? wd) (word wd ’ay) (pigl (word (bf wd) (first wd))))) (define (pl-done? wd) (vowel? (first wd))) (define (vowel? letter)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (member? letter ’(a e i o u))) Pigl introduces recursion —a function that invokes itself. More about how this works next week. Another example: Remember how to play Buzz? You go around the circle counting, but if your number is divisible by 7 or has a digit 7 you have to say “buzz” instead: ;;;;; In file cs61a/lectures/1.1/buzz.scm (define (buzz n) (cond ((equal? (remainder n 7) 0) ’buzz) ((member? 7 n) ’buzz) (else n))) This introduces the cond special form for multi-way choices. Cond is the big exception to the rule about the meaning of parentheses; the clauses aren’t invocations. 268...
View Full Document

This note was uploaded on 02/17/2010 for the course COMPUTER S 26275 taught by Professor Harvey,b during the Spring '10 term at Berkeley.

Ask a homework question - tutors are online