-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18-8-27-- scheme practice
179 lines (117 loc) · 3.26 KB
/
18-8-27-- scheme practice
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#lang racket
(cons "hi" "somebody")
(cons 0 '())
(cons 1 (cons 10 100))
(cons 1 (cons 10 (cons 100 '())))
(cons #\I (cons "saw" (cons 3 (cons "girls" '()))))
(cons "sum of" (cons 1 (cons 2 (cons 3 (cons 4 (cons "is" (cons 10 '())))))))
(car '(0))
(car '((1 2 3) (4 5 6)))
(cdr '(1 2 3 . 4))
(cdr (cons 3 (cons 2 (cons 1 '()))))
;;make a funct to add 1 to a number x
(define add1
(λ (x)
(+ 1 x)))
(add1 6)
;;syntactic sugar
(define (addd1 x) (+ 1 x))
(addd1 8)
;;write an if statement to show reciprocal of a number n
(define (inv n)
(if (not (zero? n))
(/ n)
(0)))
(inv 5)
;;; write a procedure called "bigger than 5", it take a an integer 'n' and if
;; 'n > 5' than it returns the string "bigger than 5" otherwise returns "less than 5"
(define (big5 n)
(if (>= n 5)
"greater than"
"less than"
))
(eq? (big5 7) "greater than")
;; cond practice
;;The grade (A – D) of exam is determined by the score (score). Write a function that takes a score as an argument and returns a grade.
;;A if score ≥ 80
;;B if 60 ≤ score ≤ 79
;;C if 40 ≤ score ≤ 59
;;D if score < 40
(define grade
(λ (score)
(cond
((>= score 80) 'A)
((<= 60 score 79) 'B)
((<= 40 score 59) 'C)
(else 'D))))
(grade 81)
;;recursion
;; write a function that counts the number of list items "my-length"
(define (my-length ls)
(if (null? ls)
0
(+ 1 (my-length (cdr ls)))))
(my-length '(1 2 3 8))
;;A function that takes a list (ls) and an object (x)
;;as arguments and returns a list removing x from ls.
(define (take-away ls x)
(cond
((null? ls) '())
((eq? (car ls) x) (take-away (cdr ls) x))
(else (cons (car ls) (take-away (cdr ls) x)))))
(println "hey")
(take-away '() "cat")
;; A function that takes a list (ls) and and an object (x)
;; and returns the first position of x in ls. The position is counted from 0.
;; If x is not found in ls, the function returns #f
;; modification suggestion:
;; a function that takes a list (ls), an object (x), and a starting position (idx) which you make 0
;; cases you need to consider:
;; if list is null, return false
;; if (car ls) == x, return idx
;; else return (λ (car ls) x (+ 1 idx))
(println 'ah)
(define (ugh ls x idx)
(cond
((null? ls) #f)
((eq? (car ls) x) idx)
(else
(ugh (cdr ls) x (+ 1 idx)))))
(ugh '(1 2 3 4) 3 0)
;; tail recursion
; write fn "my-reverse" that reverses the order of list items
(define (my-rev ls res)
(if (null? ls)
res
(my-rev (cdr ls) (cons (car ls) res))))
(my-rev '(1 2 3 4) '())
;; write recursive function to return value when key (x) is searched for
(define m
'((1 2) (3 "cat") (8 9)))
(define (get m x)
(if (null? m)
'()
(let* ((pair (car m))
(key (car pair))
(value (cadr pair)))
(cond
((eq? key x) value)
(else
(get (cdr m) x))))))
(get m 1)
(get m 8)
(get m 10)
(let* ((s '(1 2 3))
(l '()))
(cons s l))
;;write a recursive function that returns a list recursively
;;(define (my-list ls)
;; (if (null? ls)
;; '()
;; (cons (car ls) (cons (my-list (cdr ls)) '()))))
;; ^ not right
(define (my-ls ls)
(if (null? ls)
'()
(cons (car ls) (my-ls (cdr ls) ))))
(my-ls '(2 3 4 5 9))