blob: b4094dbcc2fd3a86f6b1842258bd43ce5ba2db92 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#lang typed/racket
;;; zadanie 1
(: prefixes (All (a) (-> (Listof a) (Listof (Listof a)))))
(define (prefixes xs)
(if (null? xs)
(list null)
(cons xs (prefixes (cdr xs)))))
;;; zadanie 2
(struct vector2 ([x : Real] [y : Real]) #:transparent)
(struct vector3 ([x : Real] [y : Real] [z : Real]) #:transparent)
(define-type Vector (U vector2 vector3))
(define-predicate vector? Vector)
(: square (-> Real Nonnegative-Real))
(define (square x)
(if (< x 0) (* x x) (* x x)))
;;; pierwsza wersja
(: vector-length (-> Vector Nonnegative-Real))
(define (vector-length v)
(if (vector2? v)
(match v [(vector2 x y) (sqrt (+ (square x) (square y)))])
(match v [(vector3 x y z) (sqrt (+ (square x) (square y) (square z)))])))
;;; druga wersja
(: vector-length-match (-> Vector Nonnegative-Real))
(define (vector-length-match v)
(match v
[(vector2 x y) (sqrt (+ (square x) (square y)))]
[(vector3 x y z) (sqrt (+ (square x) (square y) (square z)))]))
;;; zadanie 4
(struct leaf () #:transparent)
(struct [a] node ([v : a] [xs : (Listof (Tree a))]) #:transparent)
(define-type (Tree a) (node a))
(define-predicate tree? (Tree Any))
(: flat-map (All (a) (-> (-> (Tree a) (Listof a)) (Listof (Tree a)) (Listof a))))
(define (flat-map f xs)
(if (null? xs)
null
(append (f (car xs)) (flat-map f (cdr xs)))))
(: preorder (All (a) (-> (Tree a) (Listof a))))
(define (preorder t)
(match t
[(node v xs)
(cons v (flat-map preorder xs))]))
;;; (preorder (node 1 (list
;;; (node 2 (list
;;; (node 3 '())
;;; (node 4 '())))
;;; (node 5 '())
;;; (node 'x (list
;;; (node 't (list
;;; (node 'z '()))))))))
;;; zadanie 6
|