Conversation
vrom911
left a comment
There was a problem hiding this comment.
Hi! Absolutely amazing work on the Chapter! Well done 👏🏼
src/Chapter1.hs
Outdated
| lastDigit n = error "lastDigit: Not implemented!" | ||
|
|
||
| lastDigit :: Int -> Int | ||
| lastDigit n = mod n 10 |
There was a problem hiding this comment.
Your implementation is almost correct 🆗
Unfortunately, it returns negative numbers on negative inputs because of how mod works. Sometimes corner cases can be tricky to spot and fix...
src/Chapter1.hs
Outdated
| mid x y z | ||
| | (x > y && x < z) || (x < y && x > z) = x | ||
| | (y > x && y < z) || (y < x && y > z) = y | ||
| | (z > y && z < x) || (z < y && z > x) = z |
There was a problem hiding this comment.
As we mentioned, the compiler in Haskell is very attentive to the exhaustive pattern-matching. And here it would warn you that Pattern matching is not exhaustive, as the guards have quite complicated logic, and the compiler won't be able to prove that it covers all the cases.
Because of that, you will need to add another guard – | otherwise = ..., to tell the compiler, that your pattern matching is exhaustive 🙂
In this case, it would be enough to replace the last condition check with otherwise, as we know that this is the only possibility left here 🙂
| | (z > y && z < x) || (z < y && z > x) = z | |
| | otherwise = z |
There was a problem hiding this comment.
oh right, i forgot thah jaja, i send my changes in my PR on the chapter 2, thank you so much :)
| | x < 10 = x | ||
| | x < 100 = mod x 10 | ||
| | x < 1000 = mod x 100 | ||
| | otherwise = mod x 1000 |
There was a problem hiding this comment.
You are on the right way here! You see that there is the pattern in all of this steps.
Instead of manual few steps here, you can continue doing mod x 10 here until x would be smaller than 10 :)
For that you can use recursiuon (call the same function over and over again from the body of the function itself 👌🏼
3979f75 to
990e610
Compare
Solutions for Chapter 1 🗡️, I really appreciate the feedback, thanks :)
cc @vrom911 @chshersh