Skip to content

Solutions for Chapter 1#547

Open
guntbert wants to merge 6 commits intokowainik:mainfrom
guntbert:main
Open

Solutions for Chapter 1#547
guntbert wants to merge 6 commits intokowainik:mainfrom
guntbert:main

Conversation

@guntbert
Copy link
Copy Markdown

Solutions for Chapter 1

cc @vrom911 @chshersh

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

@guntbert guntbert requested a review from vrom911 as a code owner October 17, 2022 19:15
@vrom911 vrom911 added chapter1 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/ labels Oct 17, 2022
Copy link
Copy Markdown
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 πŸ‘πŸΌ

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

Comment thread src/Chapter1.hs Outdated
firstDigit n = error "firstDigit: Not implemented!"
firstDigit :: Int -> Int
firstDigit n
| abs n < 10 = n
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

2 participants