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 Lecture 3 exercises #3

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

Conversation

kpadmasola
Copy link
Owner

Solutions for Lecture 3

cc @chshersh @vrom911

Copy link

@chshersh chshersh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, exercises to Lecture 3 are done! 🏆
You've almost finished the course 🤯

Keep up the great work and I hope you're enjoying the course so far 🤗
The finish line is near 🏁

next = error "TODO"

next :: forall a. (Bounded a, Enum a) => a -> a
next day = toEnum (nextDayNum `mod` totalEnumCount)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus points for solving all extra challenges 💯 🔝

@@ -134,11 +152,17 @@ data List1 a = List1 a [a]

-- | This should be list append.
instance Semigroup (List1 a) where

(<>) :: List1 a -> List1 a -> List1 a
(List1 x xs) <> (List1 _ ys) = List1 x (xs ++ ys)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you ignore the first element of the second list. It's interesting that this implementation passes tests 🙂 But I believe you should use all elements of both lists when you append them.

Suggested change
(List1 x xs) <> (List1 _ ys) = List1 x (xs ++ ys)
(List1 x xs) <> (List1 y ys) = List1 x (xs ++ ys)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chshersh I am confused -- even your suggested change ignores the y of second list. What am I missing?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpadmasola I haven't implemented the function completely 😉
You need to use y in there. So yeah, my suggestion is still invalid. But I believe you can finish it! 💪🏻

src/Lecture3.hs Outdated
Comment on lines 185 to 187
NoTreasure <> NoTreasure = NoTreasure
NoTreasure <> SomeTreasure t = SomeTreasure t
SomeTreasure t <> NoTreasure = SomeTreasure t

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an absolutely correct solution!

However, we can notice that these 3 cases could be reduced to 2 with simpler pattern matching!
Let's see what we can unite in here.

If some of the arguments are NoTreasure, you actually always return the second one! So you need to inspect only two cases: NoTreasure in the first position, and NoTreasure in the second one 🙂

kpadmasola and others added 5 commits January 29, 2022 21:33
Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>
Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>
Co-authored-by: Dmitrii Kovanikov <kovanikov@gmail.com>
+ also addressed review comments
@kpadmasola kpadmasola changed the title Partial solutions for Lecture 3 exercises Solutions for Lecture 3 exercises Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants