Skip to content

Commit 8cf8d95

Browse files
author
Luka Kurnjek
committed
Updating homework for lesson 9.
1 parent d54968a commit 8cf8d95

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Homework/Homework09/Homework09.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11

22
-- Question 1
3-
-- You have a list of names defined below. If you use the sort function on the list
4-
-- the elements are sorted by their first name. The function uses the compare function
5-
-- from the Ord type class. Try to implement the Ord type class for the FullName type
6-
-- such that sort function will sort the elements regarding to their last name.
3+
-- Below you have a data type defined that holds a persons information. If you are deriving
4+
-- the Ord class and use the sort function on the unsortedPersons list the persons are sorted
5+
-- by their name from smallest to largest first letter.
6+
-- Implement the Ord type class for the PersonData type such that sort function will sort the
7+
-- persons regarding to their age from largest to smallest.
78

89
import Data.List (sort)
9-
newtype FullName = Name (String, String) deriving (Show, Eq)
1010

11-
unsortedNames :: [FullName]
12-
unsortedNames = [Name ("Mark","Knopfler"),Name ("Jimmy","Page"),Name ("Brian","May")]
11+
type Name = String
12+
type Age = Int
13+
type Height = Int
1314

14-
-- sort unsortedNames -- without implementing the Ord type class for FullName type
15-
-- [("Brian","May"),("Jimmy","Page"),("Mark","Knopfler")]
15+
newtype PersonData = PersonData (Name, Age, Height) deriving (Show, Eq)
1616

17-
-- Question 2
18-
-- The Enum type class has the function toEnum and fromEnum that let you convert
19-
-- user defined types into Int and vice versa. For the type MyGrades below we
20-
-- derive Enum. Implement for this type the Eq and Ord type classes by using one
21-
-- of the Enum functions.
17+
unsortedPersons :: [PersonData]
18+
unsortedPersons = [ PersonData ("Mark", 65, 173)
19+
, PersonData ("Jimmy", 45, 182)
20+
, PersonData ("Brian", 55, 178)]
21+
22+
-- sort unsortedPersons -- when deriving the Ord type class for PersonData type
23+
-- [PersonData ("Brian",55,178),PersonData ("Jimmy",45,182),PersonData ("Mark",65,173)]
2224

23-
data MyGrades = A | B | C deriving Enum
2425

25-
-- Question 3
26+
-- Question 2
2627
-- Create the type "Position" that can have the values: Intern, Junior, Senior, Manager, Chief.
2728
-- Then create the type Experience that can have the values: Programming, Managing, Leading.
2829
-- Create a function that takes in two candidates that have a Experience value and years of experience
2930
-- provided as an integer. And the function should returs the position apropriate for the candidate
30-
-- and also said which candidate has priority for employment (The higher Position gives higher
31+
-- and also say which candidate has priority for employment (The higher Position gives higher
3132
-- priority and for same positions the years of experience can be compared). Test the function on a
3233
-- set of three candidates that have experience and years: Programming 7, Programming 8, Managing 5.
3334

0 commit comments

Comments
 (0)