Conversation
vrom911
left a comment
There was a problem hiding this comment.
Very nice job on the Chapter! Congrats ๐
| -} | ||
| next :: Int -> Int | ||
| next x = error "next: not implemented!" | ||
| next x = (x + 1 ) |
There was a problem hiding this comment.
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.
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.
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.
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.
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 ๐
Solutions for Chapter 1
cc @vrom911 @chshersh