;; simple expressions '(1 2 3) (= 4 5) (- 54 3) (+ 4 5 23) (eq? #t #t) (eq? #t #f) (car '(1 2 3)) (first '(1 2 3)) (cdr '(1 2 3)) (rest '(1 2 3)) (cdar '((1 2) 3 5)) (cadr '((1 2) 3 5)) ;; variable pi (define pi 3.14159) pi ;; lambdas (lambda (x y) (* 2 (- x y))) ((lambda (x y) (* 2 (- x y))) 5 1) (define double-diff (lambda (x y) (* 2 (- x y)))) (double-diff (- 2 3) (/ 2 3)) (define (double-diff2 x y) (* 2 (- x y))) (double-diff2 (- 2 3) (/ 2 3)) ;; area of a circle (define (circle-area r) (* pi r r)) ;; function that takes an atom and attempts to respond to it appropriately (define (response s) (cond ((eqv? s 'hello) "Hi there.") ((eqv? s 'goodbye) "Until next time.") ((eqv? s 'time) "The time is 10:00AM.") (else "I Don't Understand."))) ;; recursive functions ;; function that sums elements in a list (define (sum l) (if (null? l) 0 (+ (car l) (sum (cdr l))))) ;; function that sums elements in a list (define (sum2 l) (if (null? l) 0 (+ (first l) (sum (rest l))))) ;; returns the nth fibonacci number (define (fib n) (if (eqv? n 1) 1 (if (eqv? n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))) ;; returns the nth fibonacci number (define (fib2 n) (cond ((= n 1) 1) ((= n 2) 1) (else (+ (fib2 (- n 1)) (fib2 (- n 2))))))