Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

SevroAuBarca
Copy link

Solutions for Chapter 1 🗡️, I really appreciate the feedback, thanks :)

cc @vrom911 @chshersh

@vrom911 vrom911 added chapter1 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/ labels Oct 10, 2022
Copy link
Member

@vrom911 vrom911 left a 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
Copy link
Member

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
Copy link
Member

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 🙂

Suggested change
| (z > y && z < x) || (z < y && z > x) = z
| otherwise = z

Copy link
Author

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 :)

Comment on lines +655 to +658
| x < 10 = x
| x < 100 = mod x 10
| x < 1000 = mod x 100
| otherwise = mod x 1000
Copy link
Member

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 👌🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chapter1 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants