-
-
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
Finished Chapter 1 #571
base: main
Are you sure you want to change the base?
Finished Chapter 1 #571
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.
Very nice job on the Chapter! Congrats 🎉
@@ -449,7 +450,7 @@ Implement the function that takes an integer value and returns the next 'Int'. | |||
function body with the proper implementation. | |||
-} | |||
next :: Int -> Int | |||
next x = error "next: not implemented!" | |||
next x = (x + 1 ) |
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.
The brackets are not necessary here
next x = (x + 1 ) | |
next x = x + 1 |
| n `mod` 10 == 0 = 0 | ||
| otherwise = n `mod` 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.
Nice! It could be simplified a bit though :)
This is actually could be combined into just one case, because in case of n mod 10
equal 0 it still returns 0 which is covered by just returning n mod 10
.
While doing this simplification, you can notice that it actually could be just 1 case if you use abs
function on the input 👌🏼
mid x y z | ||
| (y <= x && x <= z) || (z <= x && x <= y) = x | ||
| (x <= y && y <= z) || (z <= y && y <= x) = y | ||
| (x <= z && z <= y) || (y <= z && 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 use another guard – | otherwise = ...
, to tell the compiler, that your pattern matching is exhaustive 🙂
| (x <= z && z <= y) || (y <= z && z <= x) = z | |
| otherwise = z |
isVowel c | ||
| elem c "aeiou" = True | ||
| otherwise = False |
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! 👍🏼
As elem
returns Bool
itself, you can just return the result of this function:
isVowel c = elem c "aeiou"
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 🐎
first = div last 10 | ||
second = mod last 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 😎
One hint to make your solution even shorter: 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 🙂
Solutions for Chapter 1
cc @vrom911 @chshersh