CS99 Lecture4: Expressions and Statements - week of 9/11 Statements - like sentences - each item in sentence is a token - use language elements to build statements - classifications - expression * - assignment * - empty - quit - selection - repetition - save - read - (*) demo this today * Expression - like an operation, equation, formula - something you want Maple to evaluate - build using tokens (operators, integers, names, etc) - entering an expression is an EXPRESSION STATEMENT ex) > 1+1; Expression Tree - use precedence and associativity - decompose, or parse, tree into nodes and branches - usually the nodes are operators, functions, numbers * Assignments - name := value (LHS) (RHS) - ways to say: name <- value name "gets" value name "stores" value assign to name the value assign the value to name - sometimes NAME called a VARIABLE because assignment can change value - other things to assign name := expr (Maple evaluates expr, computes a result, stores in name) name1 := name2 (Maple stores the symbolic name2 in name1) - in fact, Maple has special ability: you may use a variable BEFORE assigning it!!! - checking assignments > var - remembering assignments assigned values persist until quitting Maple, assigning a new value, entering restart, or removing an assignment - unassigning: > var := 'var' - "=" vs ":=" "=" is an equation, which is an expression ":=" is an assignment! Worksheet Management - need to know rules to handle assignments - restart: clear all assignments - kernel modes - shared (default) -- all worksheet know all values - parallel -- each worksheet runs independently - anames() -- show list of assigned names Evaluating Expressions - must understand how Maple's "brain" works - automatic simplification and full evaluation Automatic Simplification - reduce "clearly" identical expressions - > x-x; x+x; x/x; - sometimes hard to predict Full evaluation - how Maple calculates (evaluates) results of an expression - process: - convert expression into tree - replace all assigned names with expressions, which are also converted to trees - perform automatic simplification - perform mathematical operations and functions for subexpressions until reaching the final answer - works from bottom to top Assignments Revisted - how does Maple treat assignments during full evaluation? > expr; fully evaluate expr > name:=expr fully evaluate expr and store result in name - what if expr contains a name n? if n is assigned, the assigned expression replaces n if n unassigned, n remains symbolic But Maple does not change any assignment INSIDE expr! example) > restart: x:=y+z: y:=1: x; output: 1+z explanation) clear assignments x gets y + z y gets 1 (the expression for x is still y+z: the assignment y:=1 only changed y) evaluate x (Maple replaces y with 1 inside the expression for x DURING full evaluation. So, x fully evaluates to 1+z, but x is NOT assigned to 1+z) - Example 1: see figure 3-6 - Example 2: see figure 3-7 Unevaluation - single quotes '' delay evaluation - 'expr' tells Maple to first strip 'expr' of the quotes - reason why unassignment works > a := 'a': Maple strips left side of quotes, then Maple sees a:=a internally. So, the name a gets assigned to name a.