PasteRack.org
Paste # 18865
2019-04-21 21:16:18

Forked from paste # 74914.

Fork as a new paste.

Paste viewed 135 times.


Embed:

  1. #lang racket
  2.  
  3. (define make-tswb
  4.   (lambda ()
  5.     (let ((records '()))
  6.       (lambda (command . args)
  7.         (cond
  8.           ((equal? command 'empty?)
  9.            (null? records))
  10.           ((equal? command 'add!)
  11.            (set! records (cons (car args) records)))
  12.           ((equal? command 'get)
  13.            (letrec ((sort-records (lambda (r) (sort r (lambda (x y) (<= (car x) (car y)))))))
  14.              (if (null? args)
  15.                  (sort-records records)
  16.                  (sort-records (filter (car args) records)))))
  17.           ((equal? command 'clear!)
  18.            (set! records '()))
  19.           ((equal? command 'analytic)
  20.            (if (< (length args) 2)
  21.                ((car args) records)
  22.                ((car args) (filter (cadr args) records)))))))))
  23.  
  24.  
  25. (define list-average
  26.   (lambda (x)
  27.     (/ (apply + x) (length x))))
  28.  
  29. (define average
  30.   (lambda (x)
  31.     (list-average (map cadddr x))))
  32.  
  33. (define list-sum
  34.   (lambda (x)
  35.     (if (null? x)
  36.         0
  37.         (+ (car x) (list-sum(cdr x))))))
  38.  
  39. (define sum
  40.   (lambda (x)
  41.     (list-sum (map cadddr x))))
  42.  
  43. (define (square x)
  44.   (* x x))
  45.  
  46. (define get-std-dev
  47.   (lambda (lst avg)
  48.     (cond
  49.      ((null? lst) 0)
  50.      ((+ (square (- (car lst) avg)) (get-std-dev (cdr lst) avg))))))
  51.  
  52. (define std-dev
  53.   (lambda (x)
  54.     (sqrt(/(get-std-dev (map cadddr x) (average x))(length x)))))
  55.  
  56. (define list-min
  57.   (lambda (lst num)
  58.     (cond
  59.       ((null? lst) num)
  60.       ((< num (car lst))
  61.        (list-min (cdr lst) num))
  62.        (else
  63.         (list-min (cdr lst) (car lst))))))
  64.  
  65. (define minimum
  66.   (lambda (x)
  67.     (list-min (cdr (map cadddr x))(car (map cadddr x)))))
  68.  
  69. (define list-max
  70.   (lambda (lst num)
  71.     (cond
  72.       ((null? lst) num)
  73.       ((> num (car lst))
  74.        (list-max (cdr lst) num))
  75.        (else
  76.         (list-max (cdr lst) (car lst))))))
  77.  
  78. (define maximum
  79.   (lambda (x)
  80.     (list-max (cdr (map cadddr x))(car (map cadddr x)))))
  81.  
  82. (define counter
  83.   (lambda (x)
  84.     (length x)))
  85.  
  86. (define count
  87.   (lambda (x)
  88.     (counter (map cadddr x))))
  89.  
  90. (define odd
  91.   (lambda (sorted-list num-list med)
  92.     (if (null? num-list)
  93.         med
  94.         (odd (cdr sorted-list) (cdr num-list) (car sorted-list)))))
  95.  
  96. (define even
  97.   (lambda (sorted-list num-list)
  98.     (if (equal? (length num-list) 1)
  99.         (/(+(car sorted-list)(cadr sorted-list))2)
  100.         (even (cdr sorted-list) (cdr num-list)))))
  101.  
  102. (define get-median
  103.   (lambda (sorted-list num-list)
  104.     (cond
  105.       ((odd? (length sorted-list))
  106.        (odd sorted-list num-list 0))
  107.       ((even? (length sorted-list))
  108.        (even sorted-list num-list)))))
  109.  
  110. (define median
  111.   (lambda (x)
  112.     (get-median (sort (map cadddr x) <) (range (/ (length x) 2)))))
  113.  
  114. (define range
  115.   (lambda (x)
  116.     (- (maximum x) (minimum x))))
  117.  
  118.  
  119. (define tswb (make-tswb))
  120.  
  121. (tswb 'empty?)
  122. (tswb 'add!     '(2 123 "temp1"  72.1))
  123. (tswb 'add!     '(1 123 "temp1"  72.0))
  124. (tswb 'add!     '(2 123 "press1" 29.9213))
  125. (tswb 'add!     '(1 123 "press1" 29.9212))
  126. (tswb 'add!     '(1 456 "temp1"  87.3))
  127. (tswb 'add!     '(1 456 "temp2"  87.4))
  128. (tswb 'add!     '(1 456 "press1" 28.9234))
  129. (tswb 'empty?)
  130. (tswb 'get)
  131. (tswb 'get(lambda (l) (eqv? (cadr l) 456)))                      ; return records for device 456 in time order
  132. (tswb 'get(lambda (l) (eqv? (caddr l) "temp1")))           ; return the temp1 fields for all devices
  133.  
  134. (define (temp1-123 l)
  135.   (and (eqv? (cadr l) 123) (eqv? (caddr l) "temp1")))
  136.  
  137. (tswb 'get temp1-123)                   ;get all the temp1 fields for device 123
  138. "Sum of temp1-123"
  139. (tswb 'analytic sum temp1-123)
  140. "Average"
  141. (tswb 'analytic average temp1-123)
  142. "Standard Deviation"
  143. (tswb 'analytic std-dev temp1-123)
  144. "Minimum"
  145. (tswb 'analytic minimum temp1-123)
  146. "Maximum"
  147. (tswb 'analytic maximum temp1-123)
  148. "Count"
  149. (tswb 'analytic count temp1-123)
  150. "Median"
  151. (tswb 'analytic median temp1-123)
  152. "Range"
  153. (tswb 'analytic range temp1-123)
  154. "Sum of all data"
  155. (tswb 'analytic sum)                         ;sum of all data in the database
  156. "Clearing data"
  157. (tswb 'clear!)                                ;clear the db
  158. "Empty?"
  159. (tswb 'empty?)
  160. ;(tswb 'get (lambda (l) (eqv? (cadr l) 456)))    ; return records for device 456 in time order
  161. ;(tswb 'get temp1-123)                       ; get all the temp1 fields for device 123
  162.  
  163.  
  164. ; ---------------------------------------------------------------------------------------
  165. ;(display (tswb 'get (lambda(x) (> (list-ref x 3) 72.1))))
  166.  
  167. ;(define stk (make-stack))
  168. ;stk
  169. ;(define stk2 (make-stack))
  170. ;stk2
  171. ;(sort '(2 3 4 3 1) >)

=>

#t

#f

'((1 123 "temp1" 72.0)

  (1 123 "press1" 29.9212)

  (1 456 "temp1" 87.3)

  (1 456 "temp2" 87.4)

  (1 456 "press1" 28.9234)

  (2 123 "temp1" 72.1)

  (2 123 "press1" 29.9213))

'((1 456 "temp1" 87.3) (1 456 "temp2" 87.4) (1 456 "press1" 28.9234))

'((1 123 "temp1" 72.0) (1 456 "temp1" 87.3) (2 123 "temp1" 72.1))

'((1 123 "temp1" 72.0) (2 123 "temp1" 72.1))

"Sum of temp1-123"

144.1

"Average"

72.05

"Standard Deviation"

0.04999999999999716

"Minimum"

72.0

"Maximum"

72.1

"Count"

2

"Median"

72.05

"Range"

0.09999999999999432

"Sum of all data"

407.56590000000006

"Clearing data"

"Empty?"

#t