CS 330 Lecture 34 – Scope and eval
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.
To see what work your code saves.
John Henry found out.