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

🎉 chapter1 #572

Closed
wants to merge 5 commits into from
Closed

🎉 chapter1 #572

wants to merge 5 commits into from

Conversation

CypressVillage
Copy link

Solutions for Chapter 1

cc @vrom911

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.

Bravo! Chapter number One is completed, congrats 🎉

@@ -568,7 +574,8 @@ True
>>> isVowel 'x'
False
-}
isVowel c = error "isVowel: not implemented!"
isVowel :: Char -> Bool
isVowel = (`elem` "aoeiuv")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! 👍🏼

One note in here, that sometimes, elem could be slower than the explicit pattern matching. I remember there were some benchmarks on one particular case, that showed how moving to pattern matching on each case separately drastically decrease time 🐎 But for this solution it is totally fine!

Comment on lines +645 to +646
last = ax `mod` 10
secondlast = ax `mod` 100 `div` 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a wonderful solution! 👏🏼 You correctly noticed that it is the div and mod, cool 😎

Another approach could be also using this: you can see that you use both:

mod m 10
div m 10

The 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 10

You can see how we could pattern match on the pair 🙂

src/Chapter1.hs Outdated
firstDigit :: Int -> Int
firstDigit x
| x `div` 10 == 0 = x
| otherwise = firstDigit (x `div` 10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that for a negative numbers it will be not return the correct answer. Sometimes it is tricky to keep in mind all the edge cases, so no worries, but the idea and recursive solution is awesome 👏🏼

@@ -490,7 +491,8 @@ Implement a function that returns the last digit of a given number.
whether it works for you!
-}
-- DON'T FORGET TO SPECIFY THE TYPE IN HERE
lastDigit n = error "lastDigit: Not implemented!"
lastDigit :: Int -> Int
lastDigit = (`mod` 10) . abs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's pretty!

Nice work on using infix form of calling functions! We don't explain this syntax in our course to make it short. But well done 👍

@CypressVillage
Copy link
Author

Thank you for such a careful and meticulous reply!😀 I have corrected the relevant sections

@vrom911
Copy link
Member

vrom911 commented Jan 25, 2024

No worries! It is a very great job! Feel free to get to the next Chapter ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants