-
-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 1 complete!! :D #539
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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