SHARE
TWEET

Untitled

a guest Jan 13th, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #lang racket
  2. ;1)
  3. (define (NWD a b) (if (= a b) a
  4.              (if (< a b) (NWD a (- b a))
  5.                  (NWD (- a b) b))))
  6. ;2)
  7. (define (NWW a b) (/ (* a b) (NWD a b)))
  8.  
  9. ;3)
  10. (define (same-values? f1 f2 x y)(= (f1 x y) (f2 x y)))
  11.  
  12. ;4)
  13. (define (odd? n)(cond [(zero? n) #f]
  14.                       [else (even? (- n 1))]))
  15. ;5)
  16. (define (even? n) (cond [(zero? n) #t]
  17.                        [else (odd? (- n 1))]))
  18.  
  19. ;6)
  20. (define (fib-recursive n)(if (<= n 2)
  21.                              1
  22.                              (+ (fib-recursive (- n 1)) (fib-recursive (- n 2)))))
  23.  
  24. (define (fib-iter n)(fib-iter-helper 1 0 n))
  25. (define (fib-iter-helper a b counter)(cond ((= counter 0) b)
  26.                                            (else (fib-iter-helper (+ a b) a (- counter 1)))))
  27.  
  28. ;7)
  29. (define (exp-recursive b e)(if (= e 0) 1
  30.                                 ( if (odd? e) (* b (* (exp-recursive b (/ (- e 1) 2)) (exp-recursive b (/ (- e 1) 2))))
  31.                                 (* (exp-recursive b (/ e 2)) (exp-recursive b (/ e 2))))))
  32.  
  33. (define (exp-iter b e)(exp-iter-helper b e 1))
  34. (define (exp-iter-helper b e counter) (if (= e 0) counter
  35.                                    (exp-iter-helper b (- e 1) (* counter b))))
  36.  
  37. ;8)
  38. (define (sum-of-squares-of-maxima a b c)(if (> a b)
  39.                                             (if (> b c) (+ (* a a)(* b b))
  40.                                                 (+ (* a a)(* c c)))
  41.                                             (if (> a c) (+ (* a a) (* b b))
  42.                                                 (+ (* b b)(* c c)))))
  43.  
  44. ;9)
  45. (define (call f x)(f x))
  46.  
  47. ;13)
  48. ;(define (product term a next b)())
  49.  
  50. ;17)
  51. (define (iter f n)(exp-iter f n))
  52.  
  53. ;19)
  54. ;a)
  55. (define (append l m)(if (null? l) m
  56.                         (cons (car l) (append (cdr l) m))))
  57. ;b)
  58. (define (reverse l)(if (null? l) '()
  59.                        (append (reverse (cdr l)) (list (car l)))))
  60. ;c)
  61. (define (last l)(if (null? (cdr l)) (car l)
  62.                     (last (cdr l))))
  63. ;d)
  64. (define (delete l x)(if (null? l) '()
  65.                         (if (equal? (car l) x) (delete (cdr l) x)
  66.                         (cons (car l) (delete (cdr l) x)))))
  67. ;e)
  68. (define (split l x)(split-helper l x '() '()))
  69. (define (split-helper l x l1 l2)(if (null? l) (list l1 l2)
  70.                                  (if (< (car l) x) (split-helper (cdr l) x (cons (car l) l1) l2)
  71.                                      (split-helper (cdr l) x l1 (cons (car l) l2)))))
  72.  
  73. ;20)
  74. ;b)
  75. (define (mapf l f) (if (null? l) '()
  76.                             (cons (f (car l)) (mapf (cdr l) f))))
  77. ;a)
  78. (define (square-list l) (mapf l (lambda (x) (* x x))))
  79.  
  80. ;c)
  81. (define (filter l pred)(if (null? l) '()
  82.                            (if (pred (car l)) (cons (car l) (filter (cdr l) pred))
  83.                            (filter (cdr l) pred))))
  84.  
  85. ;22)
  86. (define (ewal l)(ewal-helper (car l) (cdr l))) ; ISSUE -> nie udaje sie zastosowac apply to funkcji
  87. (define (ewal-helper exp l)(exp (ewal-resolver (car l)) (ewal-resolver (car (cdr l)))))
  88. (define (ewal-resolver x) (if (list? x) (ewal-helper (car x) (cdr x)) x))
  89.  
  90. ;23)
  91. (define (member? x l)(if (null? l) #f
  92.                          (if (equal? (car l) x) #t
  93.                              (member? x (cdr l)))))
  94.  
  95. ;24)
  96. ;INTERSECTION
  97. (define (intersection l1 l2)(if (> (length l1) (length l2)) (intersection-helper l1 l2 '()) (intersection-helper l2 l1 '())))
  98. (define (intersection-helper shorterList longerList output)(if (null? shorterList) output
  99.                                                             (if (member? (car shorterList) longerList)
  100.                                                                (intersection-helper (cdr shorterList) longerList (cons (car shorterList) output))
  101.                                                                (intersection-helper (cdr shorterList) longerList output))))
  102.  
  103. (define (union l1 l2)(union-helper (append l1 l2) '()))
  104. (define (union-helper l acc)(if (null? l) acc
  105.                                 (if (member? (car l) acc) (union-helper (cdr l) acc)
  106.                                     (union-helper (cdr l) (cons (car l) acc)))))
  107.  
  108. ;UNION
  109. (define (length l)(length-acc l 0))
  110. (define (length-acc l acc)(if (null? l) acc (length-acc (cdr l) (+ acc 1))))
  111.  
  112. ;25)
  113. (define (treeAdd x root)(if (null? root) (list '() x (list ))
  114.                             (if (< (takeRoot root) x) (append  (takeRootAndLeft root) (list (treeAdd x (takeRightSubTree root))))
  115.                                (cons (treeAdd x (takeLeftSubTree root)) (takeRootAndRight root)))))
  116.  
  117. (define (takeLeftSubTree l) (car l))
  118. (define (takeRoot l) (car (cdr l)))
  119. (define (takeRightSubTree l) (car (cdr (cdr l))))
  120. (define (takeRootAndLeft l) (list (car l) (car (cdr l))))
  121. (define (takeRootAndRight l) (cdr l))
  122.  
  123. (define (memberTree? x root) (if (null? root) #f
  124.                                  (if (= (takeRoot root) x) #t
  125.                                  (if (< (takeRoot root) x) (memberTree? x (takeRightSubTree root))
  126.                                      (memberTree? x (takeLeftSubTree root))))))
  127.  
  128.  
  129. (define (sumTree root) (sumTreeHelper root 0))
  130. (define (sumTreeHelper root acc) (if (null? root) acc
  131.                                      (+ (sumTreeHelper (takeLeftSubTree root) 0) (sumTreeHelper (takeRightSubTree root)  (+ acc (takeRoot root))))))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top