Skip to content

Commit 97042b0

Browse files
authored
Added more questions and corrected previous ones
1 parent 4ca1081 commit 97042b0

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

Homework03/Solution.hs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1-
21
-- Question 1
3-
-- Write a function that takes in two numbers and returns their quotient such that it is not grater then 1.
4-
-- Return the number as a string and in case that the divisor is 0 return a message why the division is not
5-
-- possible. To implement this function use both guards and if-else-then statements.
2+
-- Write a function that checks if the monthly consumption of an electrical device is bigger, equal, or smaller than the maximum allowed and
3+
-- returns a message accordingly.
4+
-- The function has to take the hourly consumption of an electrical device, the hours of daily use, and the maximum monthly consumption allowed.
5+
-- (Monthly usage = consumption (kW) * hours of daily use (h) * 30 days).
6+
7+
checkConsumption consumption time maxC
8+
| monthlyC > maxC = "Penguins are disappointed in you!"
9+
| monthlyC == maxC = "Don't push it!"
10+
| otherwise = "You're good!"
11+
where
12+
monthlyC = consumption * time * 30
13+
14+
-- Question 2
15+
-- Prelude:
16+
-- We use the function `show :: a -> String` to transform any type into a String.
17+
-- So `show 3` will produce `"3"` and `show (3 > 2)` will produce `"True"`.
18+
19+
-- In the previous function, return the excess/savings of consumption as part of the message.
20+
21+
checkConsumption consumption time maxC
22+
| monthlyC > maxC = "Penguins are disappointed in you. You're wasting: " ++ show (monthlyC - maxC) ++ "kWh per month!"
23+
| monthlyC == maxC = "Don't push it!"
24+
| otherwise = "You're saving: " ++ show (maxC - monthlyC) ++ "kWh per month! Good job!!"
25+
where
26+
monthlyC = consumption * time * 30
27+
28+
-- Question 3
29+
-- Write a function that showcases the advantages of using let expressions to split a big expression into smaller ones.
30+
-- Then, share it with other students in Canvas.
31+
32+
33+
-- Question 4
34+
-- Write a function that takes in two numbers and returns their quotient such that it is not greater than 1.
35+
-- Return the number as a string, and in case the divisor is 0, return a message why the division is not
36+
-- possible. To implement this function using both guards and if-then-else statements.
637

738
guardsAndIf :: Double -> Double -> String
839
guardsAndIf a b
940
| a > b = if a /= 0 then show (a/b) else "a is larger but 0"
1041
| a < b = if b /= 0 then show (b/a) else "b is larger but 0"
1142
| otherwise = if a /= 0 then "1" else "a and b are both 0"
1243

13-
-- Question 2
44+
45+
-- Question 5
1446
-- Write a function that takes in two numbers and calculates the sum of squares for the product and quotient
15-
-- of those numbers. Write the function such that you use a where statement inside a let-in stamenet and a
16-
-- let-in stamenet inside a where statement.
47+
-- of those numbers. Write the function such that you use a where block inside a let expression and a
48+
-- let expression inside a where block.
1749

1850
invertedConstructions :: Double -> Double -> Double
1951
invertedConstructions a b = let sqrtProd = sqrt abProd where abProd = a * b

0 commit comments

Comments
 (0)