• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Oct 23rd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #lang racket
2.
4. (define (odd? n)
5.   (cond [(zero? n) #f]
6.         [else (even? (- n 1))]))
7.
9. (define (fibRek n)
10.   (cond
11.     ((= n 0) 0)
12.     ((= n 1) 1)
13.     (else (+ (fibRek (- n 1)) (fibRek(- n 2))))))
14.
15. (define (fibIt n)
16.   (fib-iter 1 0 n))
17.
18. (define (fib-iter a b count)
19.   (if (= count 0)
20.     b
21.     (fib-iter (+ a b) a (- count 1))))
22.
23. ;(fibRek 6)
24. ;(fibIt 6)
25.
27. (define (exp b e)
28.   (cond
29.     ((or (= b 1) (= e 0)) 1)
30.     ((= (modulo e 2) 0) (expt (exp b (/ e 2)) 2))
31.     (else (* b (exp b (- e 1))))))
32.
33. ;(exp 2 6)
34.
36. (define (max-two a b c)
37.   (if (>= a  b)
38.       ;; then
39.       (if (>= b c)
40.           (cons a b)
41.           (cons a c))
42.       ;; else
43.    (if (>= a c)
44.       (cons b a)
45.       (cons b c))))
46. (define (sum-of-squares x y)
47.   (+ (* x x) (* y y)))
48. (define (sum-of-squares-of-maxima a b c)
49.   (sum-of-squares (car (max-two a b c)) (cdr (max-two a b c))))
50.
52.
53. (define (call f x)
54.   (f x))
55.
56. ;(call odd? 7)
57.
58. ;zadanie 10 - kolejność sprawdzania warunków
59.
60. (define (new-if warunek alternatywa1 alternatywa2)
61.    (cond (warunek alternatywa1)
62.          (else    alternatywa2)))
63.
64. ;(new-if (> 1 2) 1 2)
65.
66.
67. ;zadanie 12 - zasięg lokalny / globalny zmiennych
68. (define m 1)
69. (define (p m)
70.   (pp 5))
71. (define (pp x)
72.   (+ x m))
73.
74. (define n 1)
75. (define (q n)
76.   (define (qq x)
77.          (+ x n))
78.   (qq 5))
79.
80. ;(p 11)
81. ;(q 11)
82.