REPL Scheme as calculator -------------------- (+ 1 2) (+ 1 2 3) nesting: (+ 1 (* 2 3)) Naming values ------------- (define size 2) size (* size 2) Naming functions ---------------- (define (square x) (* x x)) (square 10) (square 2) Aruments names don't matter to the computer: (define (square apple) (* apple apple)) Important to name them properly for humans New functions expanding scheme's vocabulary Functions within functions: (define (sum-of-squares x y) (+ (square x) (square y))) (sum-of-squares 10 2) Comments and printing --------------------- ; I am a comment Display and newline command: (display "hello")(newline) Graphics programming -------------------- Functions which are defined as part of fluxus Describe a 3D scene to the renderer Workspace : ctrl-1 ctrl-d : save as - prompts you for a filename to save to ctrl-s : saves over the current filename ctrl-l : load screen, use the cursor keys and return to navigate and select a script My first cube ------------- (define (render) (draw-cube)) (every-frame (render)) (draw-cube) : draws a cube in the current state for one frame (every-frame (funcname)) : registers a function to be called every frame Press F5 Drag mouse for camera State ----- Allows us to change how things are drawn (define (render) (colour (vector 1 0 0)) (draw-cube)) (every-frame (render)) (colour) : sets the current colour state (vector) : makes a new vector (a standard Scheme command) RGB 0->1 Animation --------- (define (render) (colour (vector (flxrnd) (flxrnd) (flxrnd))) (draw-cube)) (every-frame (render)) (flxrnd) : a fluxus command which returns a random number between 0 and 1 Sound ----- Jack setup (define (render) (colour (vector (gh 1) (gh 2) (gh 3))) (draw-cube)) (every-frame (render)) (gh) : Short for "get harmonic". The audio is processed and split into 16 frequency bands, this command gets the level for the band specified. More about state ---------------- State stack (push) : copies the current state and adds it to the top of the stack to become our new current state (pop) : removes the top of the stack, and sets the current state to the next one down 'sandwich' between push and pop pairs (define (render) (push) ; push a new state (colour (vector 1 0 0)) ; set the current colour (draw-cube) ; draw a cube (translate (vector 2 0 0)) ; move a bit in X (draw-cube) ; draw another cube (pop)) ; pop the current state (every-frame (render)) New command: (translate) : translates the current transform Translate, rotate and scale. State Hierachy -------------- (define (render) (push) ; push a top level state (colour (vector 1 0 0)) ; set the top level colour (you can think of this like a default) (push) ; push a new state (colour (vector 0 1 0)) ; override the current colour (draw-cube) ; draws a green cube (pop) ; pops the green colour state and forgets it (push) ; push a new state (translate (vector 2 0 0)) ; move a bit (just so we can actually see it) (draw-cube) ; draws a red cube (we haven't specified a colour in this state, so we take the top level colour) (pop) ; pops the current state (pop)) ; pops the top level state (every-frame (render)) Making descisions ----------------- (define (abs n) ; returns the absolute value of n (if (< n 0) ; if n is less than 0 (- n) ; return the positive n n)) ; else return n as is Recursion --------- Escher Defining a function in terms of itself Always needs an exit condition Cube row Cube tree