CS 330 Lecture 34 – Scope and eval
April 27, 2012 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs330, lectures, spring 2012.
Agenda
- final project deadline
- association lists (Scheme’s hash)
- program this
- dynamic interpretation
- sandboxing
- L-systems
Program This
Pick one:
- Write a function apply-productions-once that takes as arguments a list-of-atoms and an association list. Each atom in the list is replaced by the value for which the atom is the key in the association-list. For example,
(apply-productions-once '(a b) '((a 1 2) (b X)))
evaluates to
'(1 2 X).
- Write a function apply-productions-n-times that takes as arguments a list-of-atoms, an association list, and a number n. It applies the productions defined in the association list n times and evaluates to the fully-expanded result. For example,
(apply-productions-n-times '(A) '((A B B) (B A A)) 2) ->
(apply-productions-n-times '(B B) '((A B B) (B A A) 1) ->
(apply-productions-n-times '(A A A A) '((A B B) (B A A) 0) ->
'(A A A A).
Code
lsystem.ss
(define first car)
(define rest cdr)
(define apply-productions-once
(lambda (loa alist)
(cond
((null? loa) '())
(else (append
(rest (assoc (first loa) alist))
(apply-productions-once (rest loa) alist))))))
(define apply-productions-n-times
(lambda (loa alist n)
(cond
((= n 0) loa)
(else (apply-productions-n-times
(apply-productions-once loa alist)
alist
(- n 1))))))
(define lsystem
(lambda (start productions angle n)
(apply-productions-n-times start productions n)))
(lsystem '(F - F - F - F)
'((F F - F + F + F F - F - F + F)
(- -)
(+ +))
90
1)
Haiku
Do it once by hand.
To see what work your code saves.
John Henry found out.