#| The syntax "local" takes a parenthesized sequence of definitions (define statements), followed by 1 or more expressions. When executed it: makes the definitions executes the expressions the definitions are "in scope": can be referenced in the expressions returns the value of the last expression |# [require scheme/local] #| Create and name a very simple counting "object" (as in "object-orientation"). Represented as a no-argument function. State: number of times called (including current call) 1, 2, 3, .... Behaviour: when called, return the number of times called. |# [define counter [local [[define count 0]] [lambda [] [set! count [+ count 1]] count]]] ; A true-type font for use by build-type below. [define font-path "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"] [clear] [opacity 0.4] #| Pre-order traversal of a tree: self before child (sub) trees. Post-order traversal of a tree: child trees before self. We tried this in make-tree by moving the build-cube. |# #| [make-tree levels] shows a binary tree of cubes with levels number of levels. Shows pre-order traversal as numbers in the cubes. Precondition: levels an integer >= 0. |# [define make-tree [lambda [levels] [when [> levels 0] [with-state [scale 0.5] [colour [vector 1 0 0]] [translate [vector -0.5 -1 0]] [build-type font-path [number->string [counter]]]] [build-cube] [with-state [translate [vector -2 -2 0]] [scale 0.5] [make-tree [- levels 1]]] [with-state [translate [vector 2 -2 0]] [scale 0.5] [make-tree [- levels 1]]]]]] [make-tree 3]