-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp.rkt
75 lines (66 loc) · 2.6 KB
/
fp.rkt
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
;logic: recursive cdr and car. append
(DEFINE (reverse-general L)
(COND
((NULL? L) L) ;case to break recursion
(ELSE
(IF (LIST? (CAR L)) ;check there is list inside the list
( append (reverse-general (CDR L)) ;for list inside the list
(list(reverse-general (CAR L)))
)
(append (reverse-general (CDR L)) ;not nested list
(list (CAR L))
)
)
)
)
)
;Function to calculate sum, car and recursive cdr, ignore nested list
(DEFINE (sum-up-numbers-simple L)
(COND
((NULL? L) 0) ;case to break recursion
((NUMBER? (CAR L)) (+ (CAR L) (sum-up-numbers-simple (CDR L))))
(ELSE (sum-up-numbers-simple (CDR L)))
)
)
;Function to calculate sum, car and recursive cdr
(DEFINE (sum-up-numbers-general L)
(COND
((NULL? L) 0) ;case to break recursion
((NUMBER? (CAR L)) (+ (CAR L) (sum-up-numbers-general (CDR L))))
((LIST? (CAR L)) (+ (sum-up-numbers-general (CAR L)) (sum-up-numbers-general (CDR L))))
(ELSE (sum-up-numbers-general (CDR L))
)
)
)
;Supporting function to get min value in a list
(DEFINE (get-min L)
(COND
((NULL? (CDR L)) (CAR L)) ;exit case
(ELSE
(MIN (CAR L) (get-min (CDR L))) ;recursion finding the minimum from a list
)))
;supporting function to ignore letters
(DEFINE (ignore-char L)
(COND
((NULL? L) '()) ;case to break recursion
((NUMBER? (CAR L)) (CONS (CAR L) (ignore-char (CDR L))))
(ELSE
(ignore-char (CDR L))) ;if letter
))
;supporting function that returns a list of numbers in a list which are greater than a given number
(DEFINE (great-list L1 L2)
(COND
((NULL? L2) '()) ;case to break recursion
((> (CAR L2) L1) (CONS (CAR L2) (great-list L1 (CDR L2))))
(ELSE
(great-list L1 (CDR L2)) ;if (CAR L2) < L1
)))
;main function min-above-min
(DEFINE (min-above-min L1 L2)
(COND
((NULL? (ignore-char L1)) #F) ;case to break recursion
((NULL? L2) (get-min (ignore-char L1))) ;if L2 null
((NULL? (great-list (get-min (ignore-char L2)) (ignore-char L1))) #F) ;if no value in L1 larger than smallest in L2
(ELSE
(get-min(great-list (get-min (ignore-char L2)) (ignore-char L1)))
)))