Uploaded by Moyanah Otukile

Lab6 201903482

advertisement
Author: Moyanah Otukile
Student ID: 201903482
Date: 2020/11/12
Version of work: Lab6
***This piece of work was done under the University of Botswana policy. And I therefore affirm that this
was individual work. ***
Introduction to Haskell environment. See “getting started with Haskell” or
“Writing your first Haskell program”
Key tools:
 {editor, ghc interpretor}
 {:load, :cd, :!dir,:browse, … }
𝑒𝑑𝑖𝑑(π‘–π‘›π‘π‘Žπ‘ π‘’, 𝑖𝑠𝑖𝑛, π‘Žπ‘‘π‘‘)
π‘ π‘Žπ‘£π‘’_π‘π‘Ÿπ‘–π‘£π‘Žπ‘‘π‘’(π‘™π‘œπ‘π‘Žπ‘‘π‘–π‘œπ‘›)
π‘”β„Žπ‘π‘–(: 𝑐𝑑, ∢ ! π‘‘π‘–π‘Ÿ, ∢ 𝑐𝑑, ∢ π‘™π‘œπ‘Žπ‘‘, ∢ π‘π‘Ÿπ‘œπ‘€π‘ π‘’)
π‘ π‘’π‘π‘šπ‘–π‘‘(π‘π‘Ÿπ‘œπ‘”π‘“π‘–π‘™π‘’, π‘‘π‘œπ‘π‘“π‘–π‘™π‘’)
1. a. gps(ascore) gives the grade point for the percentage score ascore.
b. lgs(ascore) gives the letter grade for the percentage score ascore.
c. lgg(agp) gives the letter grade for grade point agp.
2. Assume have the following:
ο‚· A list L of students taking the CSI243 course indicating the scores for each piece of work for lab
ο‚· A list L1 of students, from list L, who have passed at least 3 pieces of lab work. Assume a pass
mark is a minimal score of 50% and labs hold each teaching week.
a. unique(lst) which gives the unique scores in the list lst
b. occurs(ascore, lst) which determines the occurrence of the score ascore in the list lst
c. topstuds(n,lst) which gives the top n students based on their scores in the list lst.
d. botstuds(n,lst) which gives the bottom n students based on their scores in the list lst.
e. rev(lst) which gives the bottom n students based on their scores in the list lst. reverses the
scores list lst.
3. Write a simple specification for each of the functions:
a. gps(ascore)
gps: β„•->Double
gps(integer ascore):Double
Purpose: gives the grade point for the percentage score ascore.
Precondition: ascore>=0
Postcondition:
5 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
4.5 𝑖𝑓𝑓 ≤ 75
4 𝑖𝑓𝑓 ≤ 70
3.5 𝑖𝑓𝑓 ≤ 65
3 𝑖𝑓𝑓 ≤ 60
2.5 𝑖𝑓𝑓 ≤ 55
2 ≤ 50
1.5 ≤ 45
1 ≤ 40
0.5 ≤ 35
{ 0 π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
gps
gps(ascore)
70
5 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
4.5 𝑖𝑓𝑓 ≤ 75
4 𝑖𝑓𝑓 ≤ 70
3.5 𝑖𝑓𝑓 ≤ 65
3 𝑖𝑓𝑓 ≤ 60
2.5 𝑖𝑓𝑓 ≤ 55
2 ≤ 50
1.5 ≤ 45
1 ≤ 40
0.5 ≤ 35
{ 0 π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
87
5 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
4.5 𝑖𝑓𝑓 ≤ 75
4 𝑖𝑓𝑓 ≤ 70
3.5 𝑖𝑓𝑓 ≤ 65
3 𝑖𝑓𝑓 ≤ 60
2.5 𝑖𝑓𝑓 ≤ 55
2 ≤ 50
1.5 ≤ 45
1 ≤ 40
0.5 ≤ 35
{ 0 π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
b. lgs(ascore)
lgs: β„•-> [Char]
lgs(integer ascore):Char
Purpose: gives the letter grade for the percentage score ascore.
Precondition: ascore>=0
Postcondition:
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
′𝐡 + ′ 𝑖𝑓𝑓 ≤ 75
′𝐡′𝑖𝑓𝑓 ≤ 70
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 65
′𝐢 + ′𝑖𝑓𝑓 ≤ 60
′𝐢 ′ 𝑖𝑓𝑓 ≤ 55
′𝐢 − ′ ≤ 50
′𝐷 + ′ ≤ 45
′𝐷′ ≤ 40
′𝐷 − ′ ≤ 35
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
lg
89
lgs(ascore)
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
′𝐡 + ′ 𝑖𝑓𝑓 ≤ 75
′𝐡′𝑖𝑓𝑓 ≤ 70
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 65
′𝐢 + ′𝑖𝑓𝑓 ≤ 60
′𝐢 ′ 𝑖𝑓𝑓 ≤ 55
′𝐢 − ′ ≤ 50
′𝐷 + ′ ≤ 45
′𝐷′ ≤ 40
′𝐷 − ′ ≤ 35
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 80
′𝐡 + ′ 𝑖𝑓𝑓 ≤ 75
′𝐡′𝑖𝑓𝑓 ≤ 70
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 65
′𝐢 + ′𝑖𝑓𝑓 ≤ 60
′𝐢 ′ 𝑖𝑓𝑓 ≤ 55
′𝐢 − ′ ≤ 50
′𝐷 + ′ ≤ 45
′𝐷′ ≤ 40
′𝐷 − ′ ≤ 35
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
78
c. lgg(agp)
lgg: Char->Char
lgg(Char agp):Char
Purpose: gives the letter grade for grade point agp.
Precondition: agp !=null
Postcondition:
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 5
′𝐡+′ 𝑖𝑓𝑓 ≤ 4.5
′𝐡′𝑖𝑓𝑓 ≤ 4
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 3.5
′𝐢 + ′𝑖𝑓𝑓 ≤ 3
′𝐢 ′ 𝑖𝑓𝑓 ≤ 2.5
′𝐢 − ′ ≤ 2
′𝐷 + ′ ≤ 1.5
′𝐷′ ≤ 1
′𝐷 − ′ ≤ 0.5
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
lgg
lgg(agp)
79
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 5
′𝐡+′ 𝑖𝑓𝑓 ≤ 4.5
′𝐡′𝑖𝑓𝑓 ≤ 4
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 3.5
′𝐢 + ′𝑖𝑓𝑓 ≤ 3
′𝐢 ′ 𝑖𝑓𝑓 ≤ 2.5
′𝐢 − ′ ≤ 2
′𝐷 + ′ ≤ 1.5
′𝐷′ ≤ 1
′𝐷 − ′ ≤ 0.5
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
70
′𝐴′ 𝑖𝑓𝑓 π‘Žπ‘ π‘π‘œπ‘Ÿπ‘’ ≤ 5
′𝐡+′ 𝑖𝑓𝑓 ≤ 4.5
′𝐡′𝑖𝑓𝑓 ≤ 4
′𝐡 − ′ 𝑖𝑓𝑓 ≤ 3.5
′𝐢 + ′𝑖𝑓𝑓 ≤ 3
′𝐢 ′ 𝑖𝑓𝑓 ≤ 2.5
′𝐢 − ′ ≤ 2
′𝐷 + ′ ≤ 1.5
′𝐷′ ≤ 1
′𝐷 − ′ ≤ 0.5
{ ′𝐸′ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
d. unique(lst)
unique: Elems->Elems
unique(Elems lst):Elems
Purpose: which gives the unique scores in the list lst
Precondition: length (lst)>=0
𝑖𝑠𝐼𝑛 π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑 ) ≥ 1
𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž = 0
Postcondition: {
β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
unique
unique(lst)
78
𝑖𝑠𝐼𝑛 π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑 ) ≥ 1
𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž = 0
{
β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
88
{
𝑖𝑠𝐼𝑛 π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑 ) ≥ 1
𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž = 0
β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑): π‘’π‘›π‘–π‘žπ‘’π‘’ π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑) π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
e. occurs(ascore, lst)
occurs: Elem*Elems-> β„•
occurs(Elem ascore,Elems lst):Double
Purpose: determines the occurrence of the score ascore in the list lst
Precondition: (ascore ∈ lst) ᴧ ( length(lst)>=0)
1 + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) ^π‘œπ‘›(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
Postcondition: {π‘œ + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™π‘ (𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) βŒƒ π‘œπ‘“π‘“(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
π‘œ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
occurs
occurs(ascore, lst)
89
1 + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) ^π‘œπ‘›(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
{π‘œ + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™π‘ (𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) βŒƒ π‘œπ‘“π‘“(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
π‘œ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
88
1 + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™(𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) ^π‘œπ‘›(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
{π‘œ + π‘œπ‘π‘π‘’π‘Ÿπ‘  (π‘‘π‘Žπ‘–π‘™π‘ (𝑙𝑠𝑑)) 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) βŒƒ π‘œπ‘“π‘“(β„Žπ‘’π‘Žπ‘‘(𝑙𝑠𝑑))
π‘œ π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
f. topstuds(n,lst)
topstuds:Elem*Elems->Elems
topstuds(Elem n,Elems lst ):Elems
Purpose: gives the top n students based on their scores in the list lst.
Precondition: (n>=0) ᴧ (length(lst)>=0)
Postcondition: {
π‘‘π‘Žπ‘˜π‘’ 𝑛[π‘₯|π‘₯ < −𝑙𝑠𝑑, π‘₯ > 50] 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) ≥ 6 βŒƒπ‘› ≥ 1
π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
g. botstuds(n,lst)
botstuds: β„• x Elems->Elems
botstuds(Elem n ,Elems lst):Elems
Purpose: gives the bottom n students based on their scores in the list lst.
Precondition: (n>=0) ᴧ (length(lst)>=0)
Postcondition:{
π‘‘π‘Žπ‘˜π‘’ 𝑛[π‘₯|π‘₯ < −𝑙𝑠𝑑, π‘₯ < 50] 𝑖𝑓𝑓 π‘™π‘’π‘›π‘”π‘‘β„Ž(𝑙𝑠𝑑) ≥ 1 βŒƒπ‘› ≥ 1
π‘œπ‘‘β„Žπ‘’π‘Ÿπ‘€π‘–π‘ π‘’
Download