; fundamentals (+ 1 2 3 4) (+) (*) (- 61) (/ 10 4) (/ (+ (* (- (* 2 2 2 2 2 3 3 3) 1) 7) 10) 3) ; symbols (define a (define b 10)) (quote c) ; or 'c (define d '(define e 5)) ; square (define square (lambda (x) (* x x))) (define (square x) (* x x)) ; abs (define (abs x) (if (>= x 0) x (- 0 x))) ; sgn (define (sgn x) (cond ((< x 0) -1) ((= x 0) 0) (else 1))) ; factorial (define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1))))) ; pairs/lists (cons 1 2) (cons 1 (cons 2 nil)) (cons 2 nil) nil (cons 1 (cons 2 3)) (cons 1 (cons 2 (cons 3 nil))) (define x (cons 1 2)) (define y (cons 1 (cons 2 nil))) (car x) (car y) (cdr x) (cdr y) (cdr (cdr y)) (cons (cons 1 2) nil) (cons (cons 1 (cons 2 nil)) (cons 3 (cons 4 nil))) (pair? (cons 1 2)) (pair? (cons 1 (cons 2 nil))) (pair? (list 1 2 3)) (pair? nil) (pair? 3) (null? nil) (null? ()) (null? y) (null? (cdr (cdr y))) ; map (define (map fn lst) (if (null? lst) nil (cons (fn (car lst)) (map fn (cdr lst))))) ; tree abstraction (define (tree root branches) (cons root branches)) (define (root tree) (car tree)) (define (branches tree) (cdr tree)) (define (leaf? tree) (null? (branches tree))) ; square-tree (define (square-tree t) (tree (square (root t)) (if (leaf? t) nil (map square-tree (branches t))))) (define simple-tree (tree 1 (list (tree 2 nil) (tree 3 nil) (tree 4 nil)))) (define ex-tree (tree 1 (list (tree 2 (list (tree 3 nil))) (tree 4 nil) (tree 5 (list (tree 6 nil))))))