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

Solutions for Chapter 1 #547

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Solutions for Chapter 1 #547

wants to merge 6 commits into from

Conversation

guntbert
Copy link

Solutions for Chapter 1

cc @vrom911 @chshersh

This was real fun - thx a lot πŸ™‡β€β™‚οΈ

@vrom911 vrom911 added chapter1 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/ labels Oct 17, 2022
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.

Amazing job πŸ‘πŸΌ

src/Chapter1.hs Outdated Show resolved Hide resolved
src/Chapter1.hs Show resolved Hide resolved
src/Chapter1.hs Show resolved Hide resolved
Comment on lines +643 to +644
let digit1 = mod (abs n) 10
digit2 = mod (div (abs n) 10) 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 😎

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 πŸ™‚

Copy link
Author

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
Copy link
Member

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! πŸ’ͺ🏼

Copy link
Author

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 :-/

guntbert and others added 2 commits October 27, 2022 21:36
Co-authored-by: Veronika Romashkina <vrom911@gmail.com>
@guntbert
Copy link
Author

Thank you very much for the elaborate feedback - you are putting a lot of thoughts and work into this πŸ™‡ πŸ™

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chapter1 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants