The general syntax of let is
(let
( (var1 val1) (var2 val2) ... (var_n val_n) )
(function1)
(function2)
.
.
.
(function_n)
)
(let
((a 10) (b 20))
(+ a b)
)
(let
((a 10) (b 20) (+ *))
(+ a b)
)
(let
((a 10) (b 20) (+ *))
(+ a b)
)
(+ a b)
(let
((a 10) (b 20) (+ *))
(display (+ a b))
(newline)
)
(+ 10 20)
(let
((a 10) (b 20))
(let ((b 30))
(display (+ a b))
(display
"\n")
)
(let ((a 20))
(display (- a b))
)
)
(+ (- (* 3 a) b) (+ (* 3 a) b))
(let
( (n (* 3 a)) )
(+ (- n b) (+ n b))
)
(cons (car (list a b c)) (cdr (list a b c)))
(define a 20)
(define b 30)
(+ a b)
(lambda (var1 var2 ... varN) expr1 expr2 ... exprN)
(define cadr
(lambda (x)
(car ( cdr x) )
)
)
(define mystery
(lambda (x)
(/ (eval (append `(+) x) ) (length x) )
)
)
(mystery `(1 2 3 4 5))
(define compose
(lambda (a b)
(lambda (x y)
(a (b x y) y)
)
)
)
((compose + *) 3 2)
(define plusStar (compose + *))
(plusStar 3 2)
Write the following functions
(define sa
(lambda (x)
(* (* x x) 6)
(* (pow x 2) 6)
)
)
(sa 2)
Scheme has three main conditional statements
All of these are functions and return something
(define max
(lambda (x y)
(if (> x y)
x
y
)
)
)
(max 10 11)
(max 1 10)
(define canDivide?
(lambda (n d)
(if
(not (= d 0))
#t
#f
)
)
)
(canDivide? 20 0)
(canDivide? 20 1)
The syntax of a cond statement is
(cond
(test1 expression1 expression2 ...)
(test2 expressiona expressionb ...)
.
.
.
(testn expressioni expressionii ...)
(else expressionI expresionII ....)
)
(define grade
(lambda (score)
(cond
;;Example of changes from class
;;Output with this line would be 92
;;( (or (>= score 90) (>= score 91)) (+ 100 score ) score)
( (>= score 90) "A")
( (>= score 80) "B")
( (>= score 70) "C")
( (>= score 60) "D")
( else "F")
)
)
)
(grade 92)
A case statement is approximently the same as a switch statement in other langauges
The basic snytax is:
(case
(expression)
( (key1 key2 ... keyn) expr1 expr2 .. exprn)
( (keyA keyB ... keyN) expra exprb .. exprn)
( else expri exprii ... expr_n)
)
(define grade2
(lambda (x)
(case (eval x)
( (90 91 92 93 94 95 96 97 98 99 100) "A")
( (80 81 82 83 84 85 86 87 88 89) "B")
( (70 71 72 73 74 75 76 77 88 89) "C")
( (60 61 62 63 64 65 66 67 68 69) "D")
( else "F")
)
)
)
(define oddEven?
(lambda (number)
(if (= (mod number 2) 0)
"EVEN"
"ODD"
)
)
)
(oddEven? 20)