-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.L
More file actions
164 lines (142 loc) · 2.74 KB
/
sample.L
File metadata and controls
164 lines (142 loc) · 2.74 KB
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
163
164
fun remove_last with list =
(if isNil #list then
(Nil)
else
((!list) @ (remove_last #list))
)
in
fun sum with list =
if isNil list then
(0)
else
((!list) + (sum #list))
in
fun length with list =
if isNil list then
(0)
else
((1) + (length #list))
in
fun multiply with list1, list2 =
(if isNil list1 then
Nil
else
((!list1 * !list2) @ (multiply #list1 #list2))
)
in
fun insert_ordered with list, item =
(if isNil #list then (* all done, last item is dummy 0 *)
(item@0)
else
(if ((#(!list)) > (#item)) then
((!list) @ (insert_ordered (#list) item))
else
(item@list)
)
)
in
fun get_name with _ =
(let _ = (print "enter name:") in
(let name = readString in
name
)
)
in
fun get_gradeweights with weights =
(if isNil weights then
(0)
else
let grade =
(let _ = (print "enter grade:") in
(readInt)
)
in
(if (grade > 100) | (grade < 0) then
(let _ = (print "incorrect grade value") in
(get_gradeweights weights)
)
else
((grade * !weights) @ (get_gradeweights #weights))
)
)
in
fun get_avg_grade with weights =
let gradeweights = (get_gradeweights weights) in
((sum (gradeweights)) / (100))
in
fun gather_students with grades_list, weights, continue =
(if (continue = 0) then
(remove_last grades_list)
else
let name =
(let _ = (print "enter name:") in
(readString)
)
in
let avg_grade = (get_avg_grade weights) in
let new_grades_list =
(insert_ordered grades_list ((name)@(avg_grade)))
in
let cont_str =
(let _ = (print "enter another student (y/n)?") in
(readString)
)
in
let continue =
((cont_str = "y") | (cont_str = "yes") | (cont_str = "ye"))
in
(gather_students new_grades_list weights continue)
)
in
fun get_num_weights with _ =
(let num_weights =
(let _ = (print "number of weights?") in
(readInt)
)
in
(if (num_weights <= 0) then
(let _ = (print "number of wieghts must be positive") in
(get_num_weights 0)
)
else
(num_weights)
)
)
in
fun get_weights with num_weights =
(if (num_weights = 0) then
(Nil)
else
let weight =
(let _ = (print "enter weight:") in
(readInt)
)
in
(if (weight > 100) | (weight < 0) then
(let _ = (print "incorrect wieght value") in
(get_weights num_weights)
)
else
(weight @ (get_weights (num_weights - 1)))
)
)
in
(*
(insert_ordered (0) ((get_name 0)@(get_avg_grade (10@10@10@70))))
(get_avg_grade (10@10@10@70))
(gather_students (0) (10@10@10@70) 1)
(get_num_weights 0)
*)
let num_weights = (get_num_weights 0) in
let weights = (get_weights num_weights) in
(if ((sum weights) <> 100) then
(let _ = (print "Error: weights do not add to 100") in
0
)
else
(let output = (gather_students (0) weights 1) in
(let _ = (print output) in
output
)
)
)