Open
Conversation
vrom911
approved these changes
Oct 6, 2022
src/Chapter1.hs
Outdated
| -- DON'T FORGET TO SPECIFY THE TYPE IN HERE | ||
| lastDigit n = error "lastDigit: Not implemented!" | ||
| lastDigit :: Int->Int | ||
| lastDigit n = mod n 10 |
Member
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
Comment on lines
647
to
648
| let last = mod n 10 | ||
| x = div n 10 |
Member
There was a problem hiding this comment.
That is a wonderful solution! 👏🏼 You correctly noticed that it is the div and mod, cool 😎
One hint to make your solution even shorter: you can see that you use both:
mod m 10
div m 10The standard library has the divMod function, that actually combines inside both div and mod. And this is exactly what you use!.
So you could write it this way:
(x, y) = divMod m 10You can see how we could pattern match on the pair 🙂
Co-authored-by: Veronika Romashkina <[email protected]>
Co-authored-by: Veronika Romashkina <[email protected]>
Author
|
Thank you so much for the valuable feedback I have made the necessary changes as per your suggestion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solutions for Chapter 1
cc @vrom911 @chshersh