Advertisement
dgulczynski

mp2

Feb 28th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 4.61 KB | None | 0 0
  1. #lang racket
  2. (define (dist x y)
  3.   (abs (- x y)))
  4.  
  5. (define (average x y)
  6.   (/ (+ x y) 2))
  7.  
  8. (define (square x)
  9.   (* x x))
  10.  
  11. (define (inc i)
  12.   (+ i 1))
  13.  
  14. (define (cube n)
  15.   (* n n n))
  16.  
  17. ;; silnia jako procedura rekurencyjna
  18. (define (fact n)
  19.   (if (= n 0)
  20.       1
  21.       (* n (fact (- n 1)))))
  22.  
  23. ;; i jako procedura iteracyjna
  24. (define (fact-iter n i p)
  25.   (if (= n i)
  26.       p
  27.       (fact-iter n (inc i) (* p (inc i)))))
  28.  
  29. ;; równoważność dwóch definicji przez indukcję, wymaga uogólnienia twierdzenia do
  30. ;; dla każdego n, jeśli (natural? n) to dla każdego i (= (fact (+ n i)) (fact-iter (+ n i) i (fact i)))
  31.  
  32. ;; rekurencyjna definicja ciągu Fibonacciego: rekurencyjne wywołania mogą się rozgałęziać
  33. (define (fib n)
  34.   (cond [(= n 0) 0]
  35.         [(= n 1) 1]
  36.         [else    (+ (fib (- n 1)) (fib (- n 2)))]))
  37.  
  38. ;; trzy przykłady sumowania ciągów liczbowych
  39. (define (sum-ints s e)
  40.   (if (> s e)
  41.       0
  42.       (+ s (sum-ints (inc s) e))))
  43.  
  44. (define (sum-cubes s e)
  45.   (if (> s e)
  46.       0
  47.       (+ (cube s) (sum-cubes (inc s) e))))
  48.  
  49. (define (sum-pi n e)
  50.   (if (> n e)
  51.       0
  52.       (+ (/ 1 (* n (+ n 2))) (sum-pi (+ n 4) e))))
  53.  
  54. ;; ogólna procedura: abstrakcja wyższego rzędu wyrażająca sumowanie wyrazów *pewnego* szeregu
  55. (define sum
  56.   (lambda (term next s e)
  57.     (if (> s e)
  58.         0
  59.         (+ (term s) (sum term next (next s) e)))))
  60.  
  61. (define (identity x) x)
  62.  
  63. ;; alternatywny zapis ostatniego z szeregów: zamiast sum-pi-next używamy formy lambda — nienazwanej procedury
  64. (define (sum-pi-alt n e)
  65.   (define (sum-pi-term s)
  66.     (/ 1 (* s (+ s 2))))
  67.   (define (sum-pi-next s)
  68.     (+ s 4))
  69.   (sum sum-pi-term (lambda (s) (+ s 4)) n e))
  70.  
  71.  
  72. (define (close-enough? x y)
  73.   (< (dist x y) 0.00001))
  74.  
  75. ;; obliczanie (przybliżonego) punktu stałego funkcji f przez iterację, let pozwala uniknąć powtarzania obliczeń
  76. (define (fix-point f x0)
  77.   (let ((x1 (f x0)))
  78.     (if (close-enough? x0 x1)
  79.         x0
  80.         (fix-point f x1))))
  81.  
  82. ;; próba obliczania pierwiastka kwadratowego z x jako punktu stałego funkcji y ↦ x / y zapętla się
  83. ;; stosujemy tłumienie z uśrednieniem: procedurę wyższego rzędu zwracającą procedurę jako wynik
  84. (define (average-damp f)
  85.   (lambda (x) (/ (+ x (f x)) 2)))
  86.  
  87. (define (sqrt-ad x)
  88.   (fix-point (average-damp (lambda (y) (/ x y))) 1.0))
  89.  
  90. ;; obliczanie pochodnej funkcji z definicji przyjmując dx za "odpowiednio małą" wartość (zamiast "prawdziwej" granicy)
  91. (define (deriv f)
  92.   (let ((dx 0.000001))
  93.     (lambda (x) (/ (- (f (+ x dx)) (f x)) dx))))
  94.  
  95. ;; przekształcenie Newtona: x ↦ x - f(x) / f'(x) pozwala obliczyć miejsce zerowe f jako punkt stały tej transformacji
  96. (define (newton-transform f)
  97.   (lambda (x)
  98.     (- x
  99.        (/ (f x)
  100.           ((deriv f) x)))))
  101.  
  102. (define (newtons-method f x)
  103.   (fix-point (newton-transform f) x))
  104.  
  105. ;; zastosowania
  106. (define pi (newtons-method sin 3))
  107.  
  108. (define (sqrt-nm x)
  109.   (newtons-method (lambda (y) (- x (square y))) 1.0))
  110.  
  111. ;; możemy wyabstrahować wzorzec widoczny w definicjach sqrt: znaleźć punkt stały pewnej funkcji *przy użyciu* transformacji
  112. ;; argumentem fix-point-of-transform jest procedura przetwarzająca procedury w procedury!
  113. (define (fix-point-of-transform transform f x)
  114.   (fix-point (transform f) x))
  115.  
  116. ;; ==========================================
  117.  
  118. (define (compose f g)
  119.   (lambda (x) (f (g x) ) ) )
  120.  
  121. (define (repeated p n)
  122.   (if (= n 0)
  123.       identity
  124.       (compose p (repeated p (- n 1)))))
  125.  
  126. (define (term-pi x)
  127.   (/ (* (+ x 1) (- x 1))
  128.      (square x)))
  129.  
  130. (define product
  131.   (lambda (term next s e)
  132.     (if (> s e)
  133.         1
  134.         (* (term s) (product term next (next s) e)))))
  135. (define product-iter
  136.   (lambda (term next s e current)
  137.     (if (> s e)
  138.         current
  139.         (product-iter term next (next s) e (* current (term s))))))
  140.  
  141. (define accumulate
  142.   (lambda (combiner null-value term a next b)
  143.     (if (> a b)
  144.         null-value
  145.         (combiner (term a) (accumulate combiner null-value term (next a) next b)))))
  146. (define accumulate-iter
  147.   (lambda (combiner null-value term a next b )
  148.     (if (> a b)
  149.         null-value
  150.         (accumulate-iter combiner (combiner null-value (term a)) term (next a) next b ))))
  151.  
  152. (define (cont-frac num den k)
  153.   (define (cont-helper num den k i)
  154.     (if (> i k)
  155.         0
  156.         (/ (num i) (+ (den i) (cont-helper num den k (+ 1 i))))))
  157.   (if (= k 0)
  158.       0
  159.       (cont-helper num den k 1)))
  160.  
  161. (define (cont-frac-iter num den k)
  162.   (define (cont-helper num den k current)
  163.     (if (= k 0)
  164.         current
  165.         (cont-helper num den (- k 1) (/ (num k) (+ (den k) current)))))
  166.   (cont-helper num den k 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement