forked from dalvescb/LearningHaskell_Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercises02.hs
More file actions
87 lines (76 loc) · 4.55 KB
/
Exercises02.hs
File metadata and controls
87 lines (76 loc) · 4.55 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
{-|
Module : HaskellExercises02.Exercises02
Copyright : (c) Curtis D'Alves 2020
License : GPL (see the LICENSE file)
Maintainer : none
Stability : experimental
Portability : portable
Description:
Haskell exercise template Set 02 - McMaster CS 1JC3 2021
-}
module Exercises02 where
import Prelude hiding ((||),(&&),abs)
-----------------------------------------------------------------------------------------------------------
-- INSTRUCTIONS README!!!
-----------------------------------------------------------------------------------------------------------
-- 1) DO NOT DELETE/ALTER ANY CODE ABOVE THESE INSTRUCTIONS
-- 2) DO NOT REMOVE / ALTER TYPE DECLERATIONS (I.E THE LINE WITH THE :: ABOUT THE FUNCTION DECLERATION)
-- IF YOU ARE UNABLE TO COMPLETE A FUNCTION, LEAVE IT'S ORIGINAL IMPLEMENTATION (I.E. THROW AN ERROR)
-- 3) MAKE SURE THE PROJECT COMPILES (I.E. RUN STACK BUILD AND MAKE SURE THERE ARE NO ERRORS) BEFORE
-- SUBMITTING, FAILURE TO DO SO WILL RESULT IN A MARK OF 0
-- 4) REPLACE macid = "TODO" WITH YOUR ACTUAL MACID (EX. IF YOUR MACID IS jim THEN macid = "jim")
-----------------------------------------------------------------------------------------------------------
macid = "TODO"
-- NOTE see the wikipedia page on truth tables https://en.wikipedia.org/wiki/Truth_table
-- (patricularly the sections Logical conjuction, Logical disjunction, etc) for a reference
-- on the different boolean operators
-- Exercise A
-----------------------------------------------------------------------------------------------------------
-- Implement Logical disjunction (OR) using pattern matching
-- NOTE feel free to change the function declaration (pattern match on the arguments), just don't change
-- the type decleration
-----------------------------------------------------------------------------------------------------------
(||) :: Bool -> Bool -> Bool
x || y = error "TODO implement ||"
-- Exercise B
-----------------------------------------------------------------------------------------------------------
-- Implement Logical conjunction (AND) using pattern matching
-----------------------------------------------------------------------------------------------------------
(&&) :: Bool -> Bool -> Bool
x && y = error "TODO implement &&"
-- Exercise C
-----------------------------------------------------------------------------------------------------------
-- Implement Logical implication using pattern matching
-----------------------------------------------------------------------------------------------------------
(==>) :: Bool -> Bool -> Bool
x ==> y = error "TODO implement ==>"
-- Exercise D
-----------------------------------------------------------------------------------------------------------
-- Implement the function abs that returns the absolute value of a number
-----------------------------------------------------------------------------------------------------------
abs :: (Num a,Ord a) => a -> a
abs x = error "TODO implement abs"
-- Exercise E
-----------------------------------------------------------------------------------------------------------
-- Implement a function that compares two floating point numbers, and returns True if they are within
-- a tolerance of 1e-4 of eachother
-- NOTE use the abs fine you just defined
-- NOTE^2 in general, you should use an operator like this instead of == on two floating point numbers
-- HOWEVER, you'll need to adjust the tolerance to suit different contexts
-----------------------------------------------------------------------------------------------------------
(=.) :: (Floating a,Ord a) => a -> a -> Bool
x =. y = error "TODO implement =."
-- Exercise F
-----------------------------------------------------------------------------------------------------------
-- Implement a function stack that takes the first element of a list and moves it to the back
-----------------------------------------------------------------------------------------------------------
stack :: [a] -> [a]
stack xs = error "TODO implement stack"
-- Exercise G
-----------------------------------------------------------------------------------------------------------
-- Implement a function halves that takes a list of integers and divides each element of the list in two
-- (using the integer division operator `div`)
-- NOTE use the map function combined with a lambda expression to do the division
-----------------------------------------------------------------------------------------------------------
halves :: Integral a => [a] -> [a]
halves xs = error "TODO implement halves"