-
-
Notifications
You must be signed in to change notification settings - Fork 842
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
🎉 chapter1 #572
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.
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") |
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.
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!
last = ax `mod` 10 | ||
secondlast = ax `mod` 100 `div` 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.
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) |
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.
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 |
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.
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 👍
Thank you for such a careful and meticulous reply!😀 I have corrected the relevant sections |
No worries! It is a very great job! Feel free to get to the next Chapter ❤️ |
Solutions for Chapter 1
cc @vrom911