-
-
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
Solutions for Chapter 1 #547
base: main
Are you sure you want to change the base?
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.
Amazing job ππΌ
let digit1 = mod (abs n) 10 | ||
digit2 = mod (div (abs n) 10) 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 π
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.
Hmm...
I fail to see how this would be simpler - I need to apply the modulo-division by 10 to the result of the first division whereas divMod
only applies it to the original value albeit as div
and as mod
at the same time.
src/Chapter1.hs
Outdated
firstDigit n = error "firstDigit: Not implemented!" | ||
firstDigit :: Int -> Int | ||
firstDigit n | ||
| abs n < 10 = n |
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.
In case of n
being negative, this would return negative result. as you are using n
here π
But you are on the right way! πͺπΌ
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.
Ouch! I ran make test-chapter1-basic
only, and hence missed the failure - should have caught this :-/
Co-authored-by: Veronika Romashkina <[email protected]>
Thank you very much for the elaborate feedback - you are putting a lot of thoughts and work into this π π |
Solutions for Chapter 1
cc @vrom911 @chshersh
This was real fun - thx a lot πββοΈ