Lecture: 11-13-2012

posted Nov 13, 2012, 6:07 AM by Samuel Konstantinovich   [ updated Nov 20, 2012, 5:45 AM ]
Announcements:
1. All solutions for the lecture are at the end of the page.

2. Because there were some issues on the exam with part 1, there will be a quiz on Friday. (Topics: list length + car/cdr + trace a list function) I will combine your quiz with your test score in some manner to help your grade.  

Do Now: Evaluate each of the following (without using Dr.Racket)

Remember: We wrote our version of length before we learned about 
sublists. We IMPROVED length to incldue sublists at a later date.
(length '(1 2 3))
(length '((1)(2)(3)))
(length '(()()()))
(length '((1 2) 3))
(length '((1 2 3)))
(length '((1 2 (3))))
(length '((1 2 3) 4))

Write an expression using a sequence of car/cdr to extract 
the value 3 from each of the following 3 lists:
(define super '(1 2 3))
(define fighting '((1 2 (3 4)(5 6))))
(define robot '( (1 (2 (3) 4)))) 

GOAL: Sentence Generation

Sentence Generator: 
-In order to do this you need the random function in Pretty Big/Swindle. NOT R5RS.
-You need to complete up to part 3 by tonight. 

We want to be able to write a basic sentence generator, you can make it more elaborate if you want to get poetic/creative.

What are the basic parts of a sentence? 
What is the simplest sentence you can make?

We would ideally start with a simple sentence like:
kitty plays
toast runs
tea steams

;You can COPY/PASTE INTO LAB STARTING HERE ###################

;1. In scheme, define a short list of nouns, and a short list of verbs. Examples below:  (SPEND AT MOST 2 minutes on this)
(define nouns '( kitty toast tea) )
(define verbs '( runs plays steams) ) 

;2. Lets find a way to choose a random element of a list
;a. You should be able to write (getNth L n), that returns the n'th element from list L, where 0 is the starting element.
;YOU SHOULD HAVE getNth before continuing 

;Remember: (random x) returns an integer from 0 to x-1. 
;b. How do you get the number of elements of a list?
;b. The list is indexed from 0 to what value?  (If a list has 5 elements, it goes from 0 to what value?)
;d.  Write a function (getOne L) that returns a random element from list L, assume L is not null. 
;HINT: Think about the range of numbers you want to get from the list. You can say  (getNth  L (random ???) ) 
;in order to pick a random element what is the range of values you want to give getNth? (if the list has 5 elements, what range should the getNth be?)
;what do you need put in the random command to get that range of values? (if you want to generate 0-10 what do you give random?)
;YOU SHOULD HAVE getOne before continuing 

;3a. We want to be able to append two words together, so we don't just want the word we want the word inside a list. This can be accomplished with the list command.  Lets make two functions using getOne and the lists you have made:
;getNoun is a function that takes no parameters, that gives you a single random noun as a list:  
;(geNoun) -> (kitty)
;(geNoun) -> (toast)
;getVerb gives you a single random verb as a list:  
;(getVerb) -> (runs)
;(getVerb) -> (steams)
;YOU SHOULD HAVE getNoun and getVerb before continuing 

;3b. If you do that then it is trivial to make a simple sentence that chooses a verb and a noun and adds them to the same list. This is because 
to join (kitty) and (runs)  we can just append:    (append '(kitty) '(runs) ) 

;Here is the simple sentence generating function:

(define (sentence)  (append (getNoun) (getVerb) )) 

;Every time you run it you should get randomized results depending on your list of words.

;(sentence) -> ( kitty runs )

;(sentence) -> ( kitty plays )
;(sentence) -> ( toast steams )

;Try testing your generator!

;You can COPY/PASTE INTO LAB ENDNG HERE ###################

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;ON YOUR OWN FOR NOW:
;If you finished up to this point, you can try to make better sentences by replacing getNoun and getVerb with new functions. We can replace a noun with a noun phrase, and replace a verb with a verb phrase. You can experiment on your own to do so. Try using adjectives and adverbs.
____

Do Now Solutions:
3
3
3
2
1
1
2
(caddr super)
(caaddr (car fighting))
(caadar(cdar robot))

Functions you should have written:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (getNth L n)
  (if (= n 0) (car L) (getNth (cdr L) (- n 1))))

(define (getOne L)
  (getNth L (random (length L))))

(define (getNoun) (list (getOne nouns)))

(define (getVerb) (list (getOne verbs)))


Comments