1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#lang scheme
 
(define (test-my-type something)
  (cond [(number? something) number?]
        [(symbol? something) symbol?]
        [(list? something)   list?  ]))
 
(define (like-the-first L)
  (filter (test-my-type (car L)) L))
 
(define (apply-all alof alon)
  (if (null? alof)
      null
      (cons ((car alof) alon)
            (apply-all (cdr alof) alon))))
 
(define (make-counter)
  (let ((count 0))
    (lambda ()
      (set! count (+ 1 count))
      (display count)
      (newline))))
 
(define (make-counter-obj)
  (let ((count 0))
    (lambda (command)
      (cond [(symbol=? command 'get) count]
            [(symbol=? command 'inc) 
             (set! count (+ 1 count))]
            [(symbol=? command 'reset)
             (set! count 0)]))))
 
(define A (make-vector 5))
(vector-set! A 3 'something)
 
(define H (make-hash))
(hash-set! H 2 'something)
(hash-set! H (list 20 #f) 'crazy!)
 
(define (fib n)
  (if (<= n 1)
      n
      (+ (fib (- n 1))
         (fib (- n 2)))))
 
(define fib-hash (make-hash))
 
(define (fib-memo n)
  (cond [(not (hash-has-key? fib-hash n))
         (hash-set! 
          fib-hash
          n
          (if (<= n 1)
              n
              (+ (fib-memo (- n 1))
                 (fib-memo (- n 2)))))])
  (hash-ref fib-hash n))
 
;; Sum of squares from 1 to n
(define (ssq n)
    (if (= n 0)
        0
        (+ (sqr n) (ssq (- n 1)))))
 
;; Sum of squares using tail recursion
(define (ssq-better n accum)
  (if (= n 0)
      accum
      (ssq-better (- n 1)
                  (+ (sqr n) accum))))
 
(define (fib-helper n i fib-of-i fib-of-i+1)
  (if (= i n)
      fib-of-i
      (fib-helper
       n
       (+ i 1)
       fib-of-i+1
       (+ fib-of-i
          fib-of-i+1))))
 
(define (fib-tail n) (fib-helper n 0 0 1))