-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet1.hs
155 lines (131 loc) · 4.77 KB
/
Set1.hs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
-- Welcome to the first exercise set of the Haskell Mooc! Edit this
-- file according to the instructions, and check your answers with
--
-- stack runhaskell Set1Test.hs
--
-- You can also play around with your answers in GHCi with
--
-- stack ghci Set1.hs
--
-- This set contains exercises on
-- * defining functions
-- * basic expressions
-- * pattern matching
-- * recursion
module Set1 where
------------------------------------------------------------------------------
-- Ex 1: define variables one and two. They should have type Int and
-- values 1 and 2, respectively.
one :: Int
one = 1
two :: Int
two = 2
------------------------------------------------------------------------------
-- Ex 2: define the function double of type Integer->Integer. Double
-- should take one argument and return it multiplied by two.
double :: Integer -> Integer
double x = x * 2
------------------------------------------------------------------------------
-- Ex 3: define the function quadruple that uses the function double
-- from the previous exercise to return its argument multiplied by
-- four.
quadruple :: Integer -> Integer
quadruple x = double (double x)
------------------------------------------------------------------------------
-- Ex 4: define the function distance. It should take four arguments of
-- type Double: x1, y1, x2, and y2 and return the (euclidean) distance
-- between points (x1,y1) and (x2,y2).
--
-- Give distance a type signature, i.e. distance :: something.
--
-- PS. if you can't remember how the distance is computed, the formula is:
-- square root of ((x distance) squared + (y distance) squared)
--
-- Examples:
-- distance 0 0 1 1 ==> 1.4142135...
-- distance 1 1 4 5 ==> 5.0
distance :: Floating a => a -> a -> a -> a -> a
distance x1 y1 x2 y2 =
let square x = x * x
in sqrt (square (abs (x2 - x1)) + square (abs (y2 - y1)))
------------------------------------------------------------------------------
-- Ex 5: define the function eeny that returns "eeny" for even inputs
-- and "meeny" for odd inputs.
--
-- Ps. have a look at the built in function "even"
eeny :: Integer -> String
eeny x = if even x then "eeny" else "meeny"
------------------------------------------------------------------------------
-- Ex 6: here's the function checkPassword from the course material.
-- Modify it so that it accepts two passwords, "swordfish" and
-- "mellon".
checkPassword :: String -> String
checkPassword "swordfish" = "You're in."
checkPassword "mellon" = "You're in."
checkPassword _ = "ACCESS DENIED!"
------------------------------------------------------------------------------
-- Ex 7: A postal service prices packages the following way.
-- Packages that weigh up to 500 grams cost 250 credits.
-- Packages over 500 grams cost 300 credit + 1 credit per gram.
-- Packages over 5000 grams cost a constant 6000 credits.
--
-- Write a function postagePrice that takes the weight of a package
-- in grams, and returns the cost in credits.
-- hlint screams at me to use guards in this exercise.
-- Well, guards haven't been taught yet.
postagePrice :: Int -> Int
postagePrice weight =
if weight <= 500
then 250
else
if weight <= 5000
then 300 + weight
else 6000
------------------------------------------------------------------------------
-- Ex 8: define a function isZero that returns True if it is given an
-- Integer that is 0, and False otherwise. Give isZero a type signature.
--
-- Use pattern matching! Don't use comparisons!
--
-- Ps. remember, the type of booleans in haskell is Bool
isZero :: Integer -> Bool
isZero 0 = True
isZero _ = False
------------------------------------------------------------------------------
-- Ex 9: implement using recursion a function sumTo such that
-- sumTo n
-- computes the sum 1+2+...+n
sumTo :: Integer -> Integer
sumTo 1 = 1
sumTo n = n + sumTo (n-1)
-- alternatively, and a lot more quicker
-- sumTo n = n * (n+1) `div` 2
------------------------------------------------------------------------------
-- Ex 10: power n k should compute n to the power k (i.e. n^k)
-- Use recursion.
power :: Integer -> Integer -> Integer
power 0 _ = 0
power 1 _ = 1
power _ 0 = 1
power n k = n * power n (k-1)
------------------------------------------------------------------------------
-- Ex 11: ilog3 n should be the number of times you can divide given
-- number by three (rounding down) before you get 0.
--
-- For example, ilog3 20 ==> 3 since
-- 20/3 = 6.66 (gets rounded down to 6)
-- 6/3 = 2
-- 2/3 = 0.666 (gets rounded down to 0)
--
-- Use recursion to define ilog3. Use the function "div" for integer
-- division. It rounds down for you.
--
-- More examples:
-- ilog3 2 ==> 1
-- ilog3 7 ==> 2
ilog3 :: Integer -> Integer
ilog3 x =
let result = div x 3
in
if result == 0 then 1
else 1 + ilog3 result