|
1 | 1 |
|
2 | 2 | -- 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. |
7 | 8 |
|
8 | 9 | import Data.List (sort)
|
9 |
| -newtype FullName = Name (String, String) deriving (Show, Eq) |
10 | 10 |
|
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 |
13 | 14 |
|
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) |
16 | 16 |
|
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)] |
22 | 24 |
|
23 |
| -data MyGrades = A | B | C deriving Enum |
24 | 25 |
|
25 |
| --- Question 3 |
| 26 | +-- Question 2 |
26 | 27 | -- Create the type "Position" that can have the values: Intern, Junior, Senior, Manager, Chief.
|
27 | 28 | -- Then create the type Experience that can have the values: Programming, Managing, Leading.
|
28 | 29 | -- Create a function that takes in two candidates that have a Experience value and years of experience
|
29 | 30 | -- 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 |
31 | 32 | -- priority and for same positions the years of experience can be compared). Test the function on a
|
32 | 33 | -- set of three candidates that have experience and years: Programming 7, Programming 8, Managing 5.
|
33 | 34 |
|
0 commit comments