-
Notifications
You must be signed in to change notification settings - Fork 0
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
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.
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) |
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.
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) |
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.
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.
(List1 x xs) <> (List1 _ ys) = List1 x (xs ++ ys) | |
(List1 x xs) <> (List1 y ys) = List1 x (xs ++ ys) |
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.
@chshersh I am confused -- even your suggested change ignores the y
of second list. What am I missing?
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.
@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
NoTreasure <> NoTreasure = NoTreasure | ||
NoTreasure <> SomeTreasure t = SomeTreasure t | ||
SomeTreasure t <> NoTreasure = SomeTreasure t |
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.
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 🙂
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
Solutions for Lecture 3
cc @chshersh @vrom911