Advertisement
Guest User

Cwiczenia 6.06.18

a guest
Jun 6th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.18 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;; cw. 1
  4.  
  5. (define (incorrect-password amount) 'incorrect-password)
  6.  
  7. (define (make-account balance password)
  8.   (define (withdraw amount)
  9.     (if (>= balance amount)
  10.         (begin (set! balance (- balance amount))
  11.                balance)
  12.         "Insufficient funds"))
  13.   (define (deposit amount)
  14.     (set! balance (+ balance amount))
  15.     balance)
  16.   (define (dispatch p m)
  17.     (if (eq? password p)
  18.         (cond ((eq? m 'withdraw) withdraw)
  19.               ((eq? m 'deposit) deposit)
  20.               (else (error "Unknown request -- MAKE-ACCOUNT"
  21.                            m)))
  22.         (incorrect-password))) ;amount)))
  23.   dispatch)
  24.  
  25. ;; cw. 2
  26.  
  27. (define (make-cycle xs)
  28.   (define (iter ys)
  29.     (if (null? (mcdr ys))
  30.         (set-mcdr! ys xs)
  31.         (iter (mcdr ys))))
  32.   (if (null? xs)
  33.       (error "Empty list!" xs)
  34.       (iter xs)))
  35.  
  36. (define l
  37.   (mcons 1 (mcons 2 null)))
  38.  
  39. ; #0 - wskaźnik/zmienna oznaczająca listę cykliczną
  40. ; na końcu #0#
  41.  
  42. ;; cw. 3
  43.  
  44. (define (has-cycle? xs)
  45.   (define (iter ys zs)
  46.     (if (or (null? zs)
  47.             (null? (mcdr zs)))
  48.         #f
  49.        (if (eq? (mcdr ys) (mcdr (mcdr zs)))
  50.            #t
  51.            (iter (mcdr ys) (mcdr (mcdr zs))))))
  52.   (iter xs xs))
  53.  
  54. ;; cw. 4
  55.  
  56. (define (make-monitored f)
  57.   (let ((counter 0))    
  58.     (define (eval args)
  59.       (begin (set! counter (+ counter 1))
  60.              (apply f args)))
  61.     (define (how-many?) counter)
  62.     (define (reset) (set! counter 0))
  63.     (define (dispatch m)
  64.       (cond [(eq? m 'eval) eval]
  65.             [(eq? m 'how-many?) how-many?]
  66.             [(eq? m 'reset) reset]
  67.             [else (error "Unknown request -- MAKE-MONITORED"
  68.                          m)]))
  69.   dispatch))
  70.  
  71. (define (m-monitored f)
  72.   (let ((counter 0))
  73.     (cons (lambda xs
  74.             (begin (set! counter (+ counter 1))
  75.                    (apply f xs)))
  76.           (lambda (x)
  77.             (if (eq? x 'how-many?)
  78.                      counter
  79.                      (set! counter 0))))))
  80.  
  81. ;; cw. 5
  82.  
  83.  
  84. ;(define (lcons x f)
  85. ;  (cons x f))
  86.  
  87. (define (lhead l)
  88.   (car l))
  89.  
  90. (define (ltail l)
  91.   ((cdr l)))
  92.  
  93. (define (ltake n l)
  94.   (if (or (null? l) (= n 0))
  95.       null
  96.       (cons (lhead l)
  97.             (ltake (- n 1) (ltail l)))))
  98.  
  99. (define (lfilter p l)
  100.   (cond [(null? l) null]
  101.         [(p (lhead l))
  102.          (lcons (lhead l)
  103.                 (lambda ()
  104.                   (lfilter p (ltail l))))]
  105.         [else (lfilter p (ltail l))]))
  106.  
  107. (define (lmap f . ls)
  108.   (if (ormap null? ls) null
  109.       (lcons (apply f (map lhead ls))
  110.              (lambda ()
  111.                (apply lmap (cons f (map ltail ls)))))))
  112.  
  113. ;; ciąg Fibonacciego
  114.  
  115. ;; spamiętywanie
  116.  
  117. (define (memo-proc proc)
  118.   (let ((already-run? false) (result false))
  119.     (lambda ()
  120.       (if (not already-run?)
  121.           (begin (set! result (proc))
  122.                  (set! already-run? true)
  123.                  result)
  124.           result))))
  125.  
  126. (define (lcons x f)
  127.   (cons x (memo-proc f)))
  128.  
  129. (define fib
  130.   (lcons 0
  131.          (lambda ()
  132.            (lcons 1
  133.                   (lambda ()
  134.                     (lmap + fib (ltail fib)))))))
  135.  
  136. ;; cw. 6
  137.  
  138. (define fact
  139.   (lcons 1
  140.          (lambda () (lmap * (integers-starting-from 1)
  141.                           fact))))
  142.  
  143. ;; cw. 7
  144.  
  145. (define (sum ll)
  146.   (define result
  147.     (lcons 0
  148.            (lambda () (lmap + ll result))))
  149.   result)
  150.  
  151. ;; cw. 8
  152.  
  153. (define (merge xs ys)
  154.   (let ((x (lhead xs))
  155.         (y (lhead ys)))
  156.     (cond [(< x y)
  157.            (lcons x (lambda () (merge (ltail xs) ys)))]
  158.           [(< y x)
  159.            (lcons y (lambda () (merge xs (ltail ys))))]
  160.           [else
  161.            (lcons x (lambda () (merge (ltail xs) (ltail ys))))])))
  162.  
  163. ;;num235
  164.  
  165. (define num2
  166.   (lcons 1
  167.          (lambda () (lmap (lambda (x) (* x 2)) num2))))
  168.  
  169. (define num23
  170.   (lcons 1
  171.          (lambda () (merge (lmap (lambda (x) (* x 2)) num23)
  172.                            (lmap (lambda (x) (* x 3)) num23)))))
  173.  
  174. (define num235
  175.   (lcons 1
  176.          (lambda () (merge
  177.                      (merge (lmap (lambda (x) (* x 2)) num235)
  178.                             (lmap (lambda (x) (* x 3)) num235))
  179.                      (lmap (lambda (x) (* x 5)) num235)))))
  180.          
  181.        
  182.  
  183. ;dzieki result odwolujemy sie do tego samego strumienia
  184. ;a w (sum ll) zawsze nowy odkladany na stercie
  185. ;dlatego (last (ltake 100000 (sum naturals))) wykona sie w 1s.
  186.  
  187.            
  188. ;; alternatywna implementacja wykorzystująca listy modyfikowalne
  189.  
  190. ;(define (lcons x f)
  191. ;  (mcons x f))
  192.  
  193. ;(define (lhead l)
  194. ;  (mcar l))
  195.  
  196. ;(define (ltail l)
  197. ;  (when (procedure? (mcdr l))
  198. ;      (set-mcdr! l ((mcdr l))))
  199. ;  (mcdr l))
  200.  
  201. ;; dodatkowy przykład: liczby pierwsze
  202.  
  203. (define (integers-starting-from n)
  204.   (lcons n (lambda () (integers-starting-from (+ n 1)))))
  205.  
  206. (define naturals (integers-starting-from 0))
  207.  
  208. (define (divisible? x y) (= (remainder x y) 0))
  209. (define no-sevens
  210.   (lfilter (lambda (x) (not (divisible? x 7)))
  211.            naturals))
  212.  
  213. (define (sieve stream)
  214.   (lcons
  215.    (lhead stream)
  216.    (lambda ()
  217.      (sieve (lfilter
  218.              (lambda (x)
  219.                (not (divisible? x (lhead stream))))
  220.              (ltail stream))))))
  221.  
  222. (define primes (sieve (integers-starting-from 2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement