EECS 111 Review 11/13/2016
Imperative Programming Prior to learning imperative programming, all expressions had some “value” Constants have their own value (i.e. 5 is 5) Variables have whatever value they were assigned Some special forms exist (define, local, lambda, if) If it’s none of the above, it’s a procedure call
That’s Nice and All… But imperatives are different For example, set! is a void function Ex: (define x 5) (set! x 10) > x 10
Define vs. Set! Set! changes an existing variable’s value while the program is executing Define makes new variables
Functional vs Imperative The only effect of a functional program is its return value Result does not depend on order Value determined entirely by inputs Imperative programs on the other hand… Many different effects Return value, variable changes, delete files, etc. Can influence other functions as well
Imperative Advantages Usually more efficient (less steps to machine code) = faster code Can be simpler Simulations (these are basically just a bunch of changes, so it makes sense) More natural
Composing Imperatives Sequencing Begin Conditionals If, when, unless, cond Repetition for-each recursion
begin Executes a bunch of expressions in order, and then returns the value of the last thing executed (begin (set! balance (+ balance amount)) balance) Would return balance after setting the balance to balance + amount
Imperative Special Forms Set! Begin When/unless cond
Debugging! I’d look at Ian’s slides on this, but let’s walk through an example!
Exercise 6 Any other questions? Any questions about the homework so far?