Home CS61A: Lecture 27
Post
Cancel

CS61A: Lecture 27

List Processing Procedures Review

  • Built-in Methods: -(append s t): Concatenates two or more lists together into a new list
    -(map f s): Call a procedures f on each element of a list and returns a new list with the results of the procedure.
    -(filter f s): Return a new list with only the elements of the old list that evaluate as true through the procedure f.
    -(apply f s): Call a procedure f with the leements of a list s as its arguments.
  • Quasiquote (`): Within a quasiquote, we can terminate where we literally interperate elements
  • Ex: Write a procedure to return how many cons cells appear in the diagram for a value s.
1
2
3
4
5
6
(define (cons-count s)
    (if (list? s) ; Check if the element is a list or not
        (+ (length s) (apply + (map cons-counts))) ; If it is a list, we add the length of each element that is a list onto the length of the list
        0 ; An element that is just itself has 0 additional cons.
    )
)

Exceptions

  • We may catch certain errors in Python and perform some other action if an error or a particular error is encountered.

Calculator Syntax

  • The Calculator language has primitive expressions and call expressions.
  • A primitive expression is a number: 2, -4 5.6
  • A call expression is a combination that begins with an opreator (+, -, *, /) followed by 0 or more expressions:
    • (+ 1 2 3)
    • (/ 3 (+ 4 5))
  • These expressions may be expressed as a tree.
  • Primitives evaluates to themselves, while a call evaluates to its argument values comboined by an operator.
1
This post is licensed under CC BY 4.0 by the author.

CS61A: Calculator

CS61A: Interpreters