CS 330 Lecture 31 – Hi, Scheme

Agenda

  • Thinking functionally
    • recursion over iteration
    • chaining vs. sequence
  • atoms, lists, and S-expressions
  • Polish notation
  • car, cdr, cons, cond
  • our first functions
    • arithmetic expressions
    • logical expressions
    • factorial
  • lambda
  • list functions
    • length
    • contains
    • nth-element
    • merge
    • remove
    • map
    • filter

TODO

Program This

Choose one or both:

  • Write contains, which takes two arguments: a list and an atom. Return true if the list contains the atom.
  • Write nth-element, which returns the the nth-element (starting at 0) of the list.

Code

stuff.scm

(define calc-tax
  (lambda (price) (* 1.055 price)))

(calc-tax 3.69)
;; ((lambda (price) (* 1.055 price)) 3.69)

(define !
  (lambda (n) 
    (cond
      ((= n 0) 1)
      (else (* n (! (- n 1)))))))

(define merge
  (lambda (a b)
    (cond
      ((null? a) b)
      ((null? b) a)
      (...))))

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *