Advertisement
Guest User

Zajecia1-scheme

a guest
Jan 21st, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.04 KB | None | 0 0
  1. ;Pierwsze zajecia Scheme
  2. (define (silnia n)
  3.   (if (< n 2)
  4.       1
  5.   (* n (silnia (- n 1))))
  6. )
  7.  
  8. (define (silniaK n k)
  9.   (if (= n 1)
  10.       (k 1)
  11.       (silniaK (- n 1) (lambda (x) (k (* n x))))
  12.   )
  13. )
  14. (define (id x) x)
  15.  
  16. (define (silniaI n)
  17.   (do ((wynik n (* wynik n))) ((< n 2) wynik) (set! n (- n 1))))
  18.  
  19. (define (fib n)
  20.   (if (<= n 2)
  21.       1
  22.   (+ (fib (- n 1)) (fib (- n 2))))
  23. )
  24.  
  25. (define (rowne x y)
  26.   (if (= x y)
  27.       (display "sa rowne")
  28.   (display "nie sa rowne"))
  29. )
  30.  
  31. (do ((i 0 (+ i 1)) (suma 0 suma)) ((> i 6) suma) (set! suma (+ suma i)))
  32.  
  33. ;potega w O(n) (zlozonosc liniowa)
  34. ;potega w O(n) w stylu kontynuacyjnym
  35.  
  36. ;NWD:
  37. (define (GCD a b)
  38.   (cond [(= b 0) a]
  39.         [else (GCD b (modulo a b))]))
  40.  
  41. ;Lata przestepne
  42. (define (rokP rok)
  43.   (or (and (zero? (remainder rok 4))
  44.            (not (zero? (remainder rok 100))))
  45.       (zero? (remainder rok 400))))
  46.  
  47. (define dniWMies (lambda (n)
  48.                     (case n
  49.                       ((2) 28)
  50.                       ((4 6 9 11) 30)
  51.                       (else 31))))
  52.  
  53. ;Drugie zajecia Scheme
  54.  
  55. (define (potegaZle a n)
  56.   (if (= n 0) 1 (* a (potegaZle a (- n 1)))))
  57.  
  58. (define (id x) x)
  59.  
  60. (define (potegaKontynuacyjnie a n k)
  61.   (if (= n 0) (k 1) (potegaKontynuacyjnie a (- n 1) (lambda (x) (k (* a x))))))
  62.  
  63. ;Listy: '(1 4 2 a (e f))
  64. ;Lista '(a b c) to drzewo:
  65. ; .
  66. ;/ \
  67. ;a  .
  68. ;  / \
  69. ; b   .
  70. ;    / \
  71. ;   c   '()
  72.  
  73. ;Długość listy:
  74. ;car - pierwszy element
  75. ;cdr - ogon listy
  76.  
  77. (define (dlugosc l)
  78.   (if (null? l) 0
  79.       (+ 1 (dlugosc (cdr l)))))
  80.  
  81. (define (dlugoscK l k)
  82.   (if (null? l) (k 0)
  83.       (dlugoscK (cdr l) (lambda (x) (k (+ 1 x))))))
  84.  
  85. ;Połączenie dwóch list:
  86. ;cons - konstruktor par (a . b) <-para
  87. (define (polacz a b)
  88.   (if (null? b) a
  89.       (if (null? a) b
  90.           (cons (car a) (polacz (cdr a) b)))))
  91.  
  92. ;Odwracanie listy
  93. (define (odwroc a)
  94.   (if (null? a) '()
  95.       (polacz (odwroc (cdr a)) (list (car a)))))
  96.  
  97. ;Odwracanie v2
  98.  
  99.  
  100. ;Odwracanie 'kontynuacyjne'
  101. ;(define (odwrocK a '())
  102. ;  (if (null? a) '()
  103. ;      (while (= a '())...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement