Home CS61A: Scheme
Post
Cancel

CS61A: Scheme

Scheme

  • Scheme is a dialect of lisp
  • Scheme programs consist of expressions:
    • Primitive expressions: 2, 3.3, true, +, quotient, …
      • “quotient” names Scheme’s built-in integer division procedure (i.e., function)
    • Combinations: (quotient 10 2), (not true), …
  • Numbers in scheme are self-evaluatin; symbols are bound to values.
  • Call expressions include an operator and 0 or more operands in parenthesis
  • A combination can also span multiple lines and the spacing doesn’t matter.
1
2
3
4
5
6
7
8
9
10
> (quotient 10 2)
5
> (quotient (+ 8 7) 5)
3
> (+ (* 3
        (+ (* 2 4)
            (+ 3 5 )))
    (+ (- 10 7)
        6))
57
  • Certain primatives have default behaviors, such as the plus and multiplication operations
1
2
3
4
> (+)
0
> (*)
1

Other built-in procedures

  • number?: returns if a number is a number or not.
  • zero?: returns if a number is zero or not.
  • integer?: returns if a number is an integer or not.

Special Forms

  • Any combination that is not a call expression is a special form:
    • If expression:
      • (if <predicate> <consequent> <alternative>)
      • First evaluate the predicate expression.
        • If true, evaluate the consequent expression.
        • Otherwise, evaluate the alternative expression.
    • And and or:
      • (and <e1> ... <en>), (or <e1> ... <en>)
    • Binding symbols:
      • (define <symbol> <expression>)
    • New procedures:
      • (define (<symbol> <formal parameters>) <body>)
  • Ex:
1
2
3
4
5
6
7
8
9
> (define pi 3.14) # The symbol pi is bound to 3.14 in the global frame
> (* pi 2)
6.28
> (define (abs x) # A procedure is created ad bound to the sumbol "abs"
    (if (< x 0) 
        (-x)
        x))
> (abs -3)
3
  • Ex: a squaring procedure
1
2
3
4
5
6
7
8
> (define (square x) (* x x))
square
> (square 16)
256
> (define (average x y) (/ (+ x y) 2))
average
> (average 3 7)
5

Recursive functions

  • We may also create recursive functions in scheme
  • Ex: square root procedure
1
2
3
4
5
6
7
8
9
10
> (define (sqrt x) 
    (define (update guess) 
        (if (= (square(guess) x) 
            guess
            (update (average guess (/ x guess)))))
        )
    (update 1))
sqrt
> (sqrt 256)
16

Lambda Expressions

  • Lambda expressions evaluate to anonymous procedures.
    • (lambda (<formal-parameters>) <body>)
      • (define (plus4 x) (+ 4 x))
      • (define plus4 (lambda (x) (+ 4 x)))
        • We bind the name plus4 to the lambda that adds 4 to a value.
1
2
> ((lambda (x y z) (+ x y square(z))) 1 2 3)
12

More Special Forms

Cond & Begin

  • The cond special form behaves like if-elif-else statements in Python
  • The following code snippets are logically equivalent
1
2
3
4
5
6
if x > 10:
    print('big')
elif x > 5:
    print('medium')
else:
    print('small')

(cond ((> x 10) (print ‘big)) ((> x 5) (print ‘medium)) (else (print ‘small)))

  • The begin special form combines multiple expressions into one expression
    • The value of a begin special form is just the value of its last subexpression
  • We do this because certain expressions such as if or cond only supports one expression for the consequential or alternative expressions.
    • begin allows us to run multiple expressions

      (cond ((> x 10) (begin (print ‘big) (print ‘guy)) (else (begin (print ‘small) (print ‘fry))))

      Let Expressions

  • The let special form binds symbols of values temporarily and only for one expression.
    • After that expression, the binding is gone.
1
2
3
4
5
(define c
    (let ((a 3) (b (+ 2 2)) )
        (sqrt (+ (* a a ) (* b b )))
    )
)
  • The values of a and b only exist within the define c statement, and are gone after that is executed.
  • define is used for permenant bindings, while most other temporary information is bound using let.

Spierpinski’s Triangle

  • We may create certain functions to do what we want in our turtle canvas
1
2
3
4
5
6
7
8
> (define (line) (fd 50))
> (define (twice fn) (fn) (fn))
> (define (repeat k fn)
    (fn)
    (if (> k 1)
        (repeat (- k 1) fn)
    )
)
  • The following draws a triangle with a certain edge
1
2
(define (tri fn)
    (repeat 3 (lambda () (fn) (lt 120))))
  • The following draws a sierpinksi’s trianglelet
    • Create a prodecude that takes in a depth d and a length k.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(define (repeat k fn)
    (fn)
    (if (> k 1)
        (repeat (- k 1) fn)
    )
)

(define (tri fn)
    (repeat 3 (lambda () (fn) (lt 120))))
    
(define (sier d k)
    (tri (lambda()
        (if (= d 1) 
            (fd k)
            (leg d k))
        )
    )
)

(define (leg d k)
    (sier (- d 1) (/ k 2))
    (penup) (fd k) (pendown)
)

This post is licensed under CC BY 4.0 by the author.

CS61A: Lecture 25

CS61A: Lecture 26