Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.68 KB | None | 0 0
  1. #lang racket
  2. ;; LISTA 1
  3. ;;* Podstawieniowy model obliczeniowy - w miejsce zmiennej podstawiamy zwianna z nia wartosc
  4. ;;* Racket liczy gorliwie********
  5. ;; '+ daje #<procedure:+>, ale (+) daje 0
  6. ;; formy specjalne 'or' i 'and'
  7.  
  8.  
  9. (define (power-close-to b n)
  10.   (define (helper b n k)
  11.     (if (> (expt b k) n)
  12.         k
  13.         (helper b n (+ k 1))))
  14.   (helper b n 0))
  15.  
  16.  
  17. ;; LISTA 2   [ NIE ZROBIŁEM ZADANIE 8 i 9 ]
  18. ;; Rozróżniać procesy rekurencyjne i iteracyjne!****
  19. ;; Składniowe wiązanie zmiennych !***;;;;;;;;;;;;!!:;;;
  20. ;; Procedury anonimowe (to lambdy) np : (lambda (i) (+ i 1))
  21. ;; Cukier składniowy(lukier syntaktyczny ) - dowolna cecha skladni jezyka ktora można wyeliminować przez proste
  22. ; przekształcenia składniowe, istniejąca tylko dla wygody programisty
  23. ;; Procedury wyższego rzędu - procedury zwracające jako wynik procedure
  24.  
  25. (define (inc i)
  26.   (+ 1 i))
  27.  
  28. (define (incc i)
  29.   (+ 2 i))
  30.  
  31. (define (square x)
  32.   (* x x))
  33. ;2
  34. (define (compose f x)
  35.   (lambda (i) (f (x i))))
  36. ;3
  37. (define (repeated p n)
  38.   (if (= n 0)
  39.       p
  40.       (compose p (repeated p (- n 1)))))
  41. ;4
  42. (define product
  43.   (lambda (term next s e op)
  44.     (if (= s e)
  45.         (term s)
  46.     (op (term s) (product term next (next s) e op)))))
  47.  
  48. (define product2
  49.   (lambda (term next s e op acc)
  50.     (if (= s e)
  51.         acc
  52.         (product2 term  next (next s) e op (op (term s) acc)))))
  53.  
  54. (define (dopi x)
  55.  (/ (* x (+ x 2)) (* (+ x 1) (+ x 1))))
  56.  
  57.  
  58.  
  59. (define (accumulate combiner s e null-value term next)
  60.   (if (= s e)
  61.      (combiner (term s) null-value)
  62.       (accumulate combiner (next s) e (combiner (term s) null-value) term next)))
  63.      
  64.  
  65. (define (cont-frac-rec num den k)
  66.   (if (= k 0)
  67.       (/ (num) (den))
  68.       (/ (num) (+ (den) (cont-frac-rec num den (- k 1))))))
  69.  
  70.  
  71. (define (pi-cont-frac num den k x)
  72.   (if (= k 0)
  73.     (/  (num x) (den))
  74.     (/ (num x) (+ (den) (pi-cont-frac num den (- k 1) (+ x 2))))))
  75.  
  76.  
  77. ;;LISTA 3 [ 6 i 7, i 8 nie zrobione]  - indukcja i permutacja
  78. ;; Selektory, konstruktory, predykaty
  79. ;;Potrafie opowiedzic o zaleznosci miedzy predykatami a konstruktorami np dla pair, listy? etc
  80.  
  81. (define (reverse-rek xs)
  82.   (if (pair? xs)
  83.       (append (reverse-rek (cdr xs)) (cons (car xs) null))
  84.       xs))
  85.  
  86. (define (fold-right op nval xs) ;; spoko funkcja
  87.   (if (null? xs)
  88.       nval
  89.       (op (car xs)
  90.           (fold-right op nval (cdr xs)))))
  91.  
  92.  
  93. (define (insert xs n)
  94.   (if (> n (car xs))
  95.       (cons (car xs) (insert (cdr xs) n))
  96.       (cons n xs)))
  97.  
  98.  
  99. (define (append-s . xs)
  100.   (fold-right append null xs))
  101.      
  102. (define (permi xs)
  103.   (if (null? xs)
  104.       null
  105.       (append-s (map (lambda (zs) (insert (car zs) zs)) (permi (cdr xs))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement