Expressions and Evaluation rules
#Rules to form an expression
; comments are ignored and are for you to jott down notes
#|this is a multi-line comment|#
(<operator> <argument-1> <argument-2> ...)
(+ 2 4) ; evaluates to the value 6! yay!where:
(+, is the name of the operator, also sometimes called a function2, is the 1st argument consumed by the operator, also sometimes called operands4, is the 2nd argument/operand)
Notice that spaces are used to seperate multiple arguments, NOT COMMAS as in other languages. If you type a comma, it will be an error.
It reads: Call the + function with arguments 2 and 4
The result of executing the function/operation/expression is a value, in this case the number 6.
#Evaluating Expressions in detail
Operands/Arguments can also be expressions themselves that then evaluate to a value
; problem: what does this evaluate to?
(+ 2 4 (* 5 5) (- (+ 3 3) 2) 1)Look from Left -> Right and see if there are expressions that are not yet values
Starting from left to right, in order for 1st + to do its job, all arguments need to be reduced to a value
2, 4, 1are already values but(* 5 5)is not a value, it’s an expression, thus we need to evaluate it resulting to 25Evaluate the sub-expressions and turn them into values
(+ 2 4 (* 5 5) (- (+ 3 3) 2) 1) ; turns into: (+ 2 4 25 (- (+ 3 3) 2) 1) ; (* 5 5) above turns into 25 ; turns into: (+ 2 4 25 (- 6 2) 1) ; (+ 3 3) above turns into 6, notice we're back out to the substraction now ; turns into: (+ 2 4 25 4 1) ; (- 6 2) above turns into 4Finally the + operator can do its job
36
Notice in the nested expression:
(- (+ 3 3) 2)after evaluating the inside: (+ 3 3), we’re back outside to the - operation, (- 6 2). So in summary the rules for evaluation are left to right from inside to out.
Also note that (- 6 2) follows the same structure for forming an expression (<operator> arg1...)
#Inexact numbers
Numbers like pi or (sqrt 2) are decimal numbers that go on infinitely but computers are limited in terms of memory. These numbers will be printed with a #i to indicate they are close but
(sqrt 2)
#i1.4142135623730951
pi
#i3.141592653589793Check your understanding!
What does (+ (- (- 9 1) 2) 7 (* 3 2) 1) evaluate to?
; problem: what does this evaluate to?
(+ (- (- 9 1) 2) 7 (* 3 3) 1)
; step 1: we first need to evaluate (- 9 1) to 8
(+ (- 8 2) 7 (* 3 3) 1)
; step 2: then evaluate (- 8 2) to 6
(+ 6 7 (* 3 3) 1)
; step 3: we're still not done yet (* 3 3) to 9
(+ 6 7 9 1)
; step 4: now we can add them all up:
23
#Errors
((+ 3 4)) this an error because there is no operator in the outer parens(3 (+ 1 6)) this is also an error because 3 is not an operator, your cannot “3” yourself.
Immediately following an opening parenthesis should ALWAYS be an operator.
We’ll cover errors more extensively later but for now just remember to make sure everything must follow (<operator> <arg1> ...)
#Practice Problems
Infix to prefix math expressions
#Exercise 0 - Pythag
#|
PROBLEM:
|\
| \
| \
5| \ ?
| \
| \
|______\
4
Two sides of a right triangle have length 3 and 4,
what is the length of the hypotenuse(longest side)?
Recall the Pythagorean Theorem solves this for us:
Reads: the square root(sqrt) of 3 * 3 + 4 * 4
______________
\/5 * 5 + 4 * 4
^ Encode the math expression above
in a BSL expression that will solve for the missing side ?
|#Answer
#|
PROBLEM:
|\
| \
| \
3| \ ?
| \
| \
|______\
4
Two sides of a right triangle have length 3 and 4,
what is the length of the hypotenuse(longest side)?
Recall the Pythagorean Theorem solves this for us:
Reads: the square root(sqrt) of 3 * 3 + 4 * 4
______________
\/3 * 3 + 4 * 4
^ Encode the math expression above
in a BSL expression that will solve for the missing side ?
|#
(sqrt (+ (* 5 5) (* 4 4)))