-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathExercises01.hs
More file actions
99 lines (87 loc) · 4.85 KB
/
Exercises01.hs
File metadata and controls
99 lines (87 loc) · 4.85 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
{-|
Module : HaskellExercises01.Exercises01
Copyright : (c) Curtis D'Alves 2020
License : GPL (see the LICENSE file)
Maintainer : none
Stability : experimental
Portability : portable
Description:
Haskell exercise template Set 01 - McMaster CS 1JC3 2021
-}
module Exercises01 where
import Prelude hiding (last,init,(!!))
-----------------------------------------------------------------------------------------------------------
-- 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: THE PRELUDE FUNCTIONS YOU'RE REQUIRED TO IMPLEMENT HAVE BEEN REMOVED, HOWEVER OTHER PRELUDE
-- FUNCTIONS ARE STILL AVAILABLE, E.I. drop,take,reverse,head,tail,length,div
-- YOU ARE ALSO FREE TO USE FUNCTIONs AFTER THEY'VE BEEN DEFINED
-- Exercise A
-----------------------------------------------------------------------------------------------------------
-- Implement the Prelude function last (which returns the last element of a list) using a combination of
-- other available Prelude functions
-----------------------------------------------------------------------------------------------------------
last :: [a] -> a
last xs = error "TODO implement init"
-- Exercise B
-----------------------------------------------------------------------------------------------------------
-- Implement the Prelude function init (which returns a list with the last element removed) using a
-- combination of other available Prelude functions
-----------------------------------------------------------------------------------------------------------
init :: [a] -> [a]
init xs = error "TODO implement init"
-- Exercise C
-----------------------------------------------------------------------------------------------------------
-- Implement the index function !! (which is used to access a specific element of a list) using a
-- combination of other Prelude functions
-- For example:
-- xs = ['a','b','c']
-- xs !! 0 == 'a'
-- xs !! 2 == 'c'
-- xs !! 4 == ERROR
-----------------------------------------------------------------------------------------------------------
(!!) :: [a] -> Int -> a
xs !! n = error "TODO implement !!"
-- Exercise D
-----------------------------------------------------------------------------------------------------------
-- Implement the functions firstHalf and lastHalf, that take a list and return the first and last halfs
-- respectively. If the list is uneven, the first half should be one element smaller than the last half
-- HINT: use the `div` function instead of / to do integer division when dividing the length of the list
-- by 2, then take or drop those amount of elements from the list
firstHalf :: [a] -> [a]
firstHalf xs = error "TODO implement firstHalf"
lastHalf :: [a] -> [a]
lastHalf xs = error "TODO implement lastHalf"
-- Exercise E
-----------------------------------------------------------------------------------------------------------
-- Implement the function inners that returns the inside of a list (i.e. without the first and last elements)
-- For example:
-- inners [] == []
-- inners [1] == []
-- inners [1,2] == []
-- inners [1,2,3,4] == [2,3]
inners :: [a] -> [a]
inners [] = []
inners xs = error "TODO implement inners"
-- Exercise F
-----------------------------------------------------------------------------------------------------------
-- Implement a function that computes the Euclidean distance in 2 dimensions of two points p and q
-- See https://en.wikipedia.org/wiki/Euclidean_distance for details
distance :: (Float,Float) -> (Float,Float) -> Float
distance (p1,p2) (q1,q2) = error "TODO implement distance"
-- Exercise F
-----------------------------------------------------------------------------------------------------------
-- Write a function nthRoot that computes the nth root (i.e. n==2 square root, n==3 cube root, etc)
-- using the fact that the nth root of a number is the same as performing a power to 1/n
-- NOTE: you have to use the ** operator instead of ^ when using floating point numbers for powers
-- you'll also need fromIntegral to convert from integers to a floating point
nthRoot :: Float -> Int -> Float
nthRoot x n = error "TODO implement nthRoot"