Scheme II

Variables in Scheme

  • Variables in scheme have two basic scopes
    • Local scope set with the let command
    • Global scope set with the define command
    • There are a few other variations of let that have slightly different scope
      • We will talk about these when we talk about recursion

Let

The general syntax of let is

(let
    ( (var1 val1) (var2 val2) ... (var_n val_n) )
    (function1)
    (function2)
    .
    .
    .
    (function_n)
)

Let Examples

In [1]:
(let 
  ((a 10) (b 20))
  (+ a b)
)
Out[1]:
30
In [2]:
(let
  ((a 10) (b 20) (+ *))
  (+ a b)
)
Out[2]:
200
In [5]:
(let
  ((a 10) (b 20) (+ *))
  (+ a b)
)
(+ a b)
Traceback (most recent call last):
  File "In [5]", line 5, col 4
RunTimeError: unbound variable 'a'

In [7]:
(let
  ((a 10) (b 20) (+ *))
  (display (+ a b))
  (newline)
)
(+ 10 20)
200
Out[7]:
30

Nested Let

  • When let is nested inside another let the variable can be redefined
In [8]:
(let
  ((a 10) (b 20))
  (let ((b 30))
    (display (+ a b))
    (display 
    "\n")
  )
  (let ((a 20))
    (display (- a b))
  )
)
40
0

Let Practice

From "The Scheme Programming Language"

Use let to remove common expressions from:

In [ ]:
(+ (- (* 3 a) b) (+ (* 3 a) b))


(let
    ( (n (* 3 a)) )
    (+ (- n b) (+ n b))
)
In [ ]:
(cons (car (list a b c)) (cdr (list a b c)))

Define

  • Define is used to make a variable have global scope
  • While it can be used for objects, it is most often used for functions
In [10]:
(define a 20)
(define b 30)
(+ a b)
Out[10]:
50

Functions

  • In Scheme, the act of defining a function and naming a function require two seperate expressions
  • To define a function, a lambda expression is used
    • The general syntax is
      (lambda (var1 var2 ... varN) expr1 expr2 ... exprN)
      
    • This will return a function (often called a procedure in scheme)
  • To name a function, use let or define
    (define cadr
      (lambda (x) 
          (car ( cdr x) ) 
      ) 
    )
    

Function Examples

In [12]:
(define mystery
  (lambda (x)
   (/ (eval (append `(+) x) ) (length x) )
  )
)
In [18]:
(mystery `(1 2 3 4 5))
Out[18]:
3
In [20]:
(define compose
  (lambda (a b)
    (lambda (x y) 
      (a (b x y) y)
    )
  )
)
In [23]:
((compose + *) 3 2)
Out[23]:
8
In [25]:
(define plusStar (compose + *))
(plusStar 3 2)
Out[25]:
8

Function Practice

Write the following functions

  • Returns the surface area of a cube given the length of side x, $6x^2$
  • A function that converts a temperature given in farenheight to celcius ($\frac{(F - 32) x 5}{9}$)
In [27]:
(define sa
  (lambda (x)
    (* (* x x) 6)
    (* (pow x 2) 6)    
  )
)
In [29]:
(sa 2)
Out[29]:
24

Conditionals

Scheme has three main conditional statements

  • An if statement
  • The cond statement
  • The case statement

All of these are functions and return something

If Statement

The syntax of an if statement is

(if
    test
    consequent
    alternative
)

If Statement Examples

In [31]:
(define max
  (lambda (x y)
    (if (> x y)
        x
        y
    )
  )
)
In [32]:
(max 10 11)
Out[32]:
11
In [33]:
(max 1 10)
Out[33]:
10
In [30]:
(define canDivide?
  (lambda (n d)
    (if
      (not (= d 0))
      #t
      #f
    )
  )
)
In [34]:
(canDivide? 20 0)
Out[34]:
#f
In [36]:
(canDivide? 20 1)
Out[36]:
#t

Cond Statement

The syntax of a cond statement is

(cond
    (test1 expression1 expression2 ...)
    (test2 expressiona expressionb ...)
    .
    .
    .
    (testn expressioni expressionii ...)
    (else expressionI expresionII ....)
)

Cond Statement Examples

In [88]:
(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)
Out[88]:
"A"

Case Statements

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)
)

Case Statement Example

In [ ]:
(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")   
   )
  )
)

Conditionals Practice

Write functions that do the following:

  • Returns if a number is odd or even
In [44]:
(define oddEven?
  (lambda (number)
    (if (= (mod number 2) 0)
        "EVEN"
        "ODD"
    )
  )
)

(oddEven? 20)
Out[44]:
"EVEN"