-
-
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
Thank You developers!! #514
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.
Fantastic work 💯
I have added some comment, hope it would be helpful 🙂
@@ -449,7 +450,7 @@ Implement the function that takes an integer value and returns the next 'Int'. | |||
function body with the proper implementation. | |||
-} | |||
next :: Int -> Int | |||
next x = error "next: not implemented!" | |||
next x = x+1 |
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.
Great!
In Haskell, all operators are separated by spaces from its arguments 👾
So it would be:
next x = x+1 | |
next x = x + 1 |
@@ -490,7 +491,7 @@ Implement a function that returns the last digit of a given number. | |||
whether it works for you! | |||
-} | |||
-- DON'T FORGET TO SPECIFY THE TYPE IN HERE | |||
lastDigit n = error "lastDigit: Not implemented!" | |||
lastDigit n = mod (abs n) 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.
Nice work!
We don't explain this in our course, but you can also use the infix form of calling functions! (Meaning, the function can be written in between of its arguments):slightly_smiling_face:
lastDigit n = abs n `mod` 10
(Note that you can remove ()
, they are redundant in that case )
You can read a bit more on the infix here: https://kowainik.github.io/posts/fixity#notation
@@ -520,7 +521,7 @@ branches because it is an expression and it must always return some value. | |||
satisfying the check will be returned and, therefore, evaluated. | |||
-} | |||
closestToZero :: Int -> Int -> Int | |||
closestToZero x y = error "closestToZero: not implemented!" | |||
closestToZero x y = if (abs x < abs y) then x else y |
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.
No need to have brackets here. You can skip it in if-then-else constructions:
closestToZero x y = if (abs x < abs y) then x else y | |
closestToZero x y = if abs x < abs y then x else y |
| y<=(max x z)&&y>=(min x z)=y | ||
| x<=(max z y)&&x>=(min z y)=x | ||
| otherwise =z |
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.
Awesome solution!
Can add spaces around operators:
| y<=(max x z)&&y>=(min x z)=y | |
| x<=(max z y)&&x>=(min z y)=x | |
| otherwise =z | |
| y <= max x z && y >= min x z = y | |
| x <= max z y && x >= min z y = x | |
| otherwise = z |
@@ -632,8 +642,7 @@ Try to introduce variables in this task (either with let-in or where) to avoid | |||
specifying complex expressions. | |||
-} | |||
|
|||
sumLast2 n = error "sumLast2: Not implemented!" | |||
|
|||
sumLast2 n = if (abs n)<10 then (abs n) else (if (abs n)<100 then (div (abs n) 10)+(mod (abs n) 10) else sumLast2 (mod (abs n) 100)) |
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, one-liner 😮 Nice recursive solution!
It is a bit difficult to read though 🙂 I would suggest to align it at different lines first:
sumLast2 n = if (abs n)<10 then (abs n) else (if (abs n)<100 then (div (abs n) 10)+(mod (abs n) 10) else sumLast2 (mod (abs n) 100)) | |
sumLast2 n = | |
if abs n < 10 | |
then abs n | |
else | |
if abs n < 100 | |
then div (abs n) 10 + mod (abs n) 10 | |
else sumLast2 (mod (abs n) 100) |
Then I see that 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 🙂
| null x=[] | ||
| otherwise=(head x):(head x):duplicate (tail x) |
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.
Here it is also better to use pattern matching instead of using unsafe head
function. (It is also more efficient 🙂 )
takeEven x | ||
| ((length x) == 0) = [] | ||
| ((length x) == 1) = head x:[] | ||
| otherwise=(head x):takeEven (tail (tail x)) |
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.
Same here 🙂
contains = error "contains: Not implemented!" | ||
|
||
--contains = error "contains: Not implemented!" | ||
contains a x= filter (elem a) x |
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.
Now, after you've mastered eta-reduction, you can apply such a technique to this function as well 🙂
But your implementation is already great 💯
|
||
-- Can you eta-reduce this one??? | ||
pairMul xs ys = zipWith (*) xs ys | ||
pairMul = zipWith (*) |
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.
Eta-reduction award 🏆
--rewind = error "rewind: Not Implemented!" | ||
rewind x | ||
| null x=[] | ||
| otherwise=(rewind (tail x))++((head x):[]) |
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.
Your solution is correct! However, it is slow. In lists, it is quite slow to add anything at the end of the list. That is why it is always better to rewrite it with the :
cons. Remember the explanation with trains? 🚂 🚋 🚋
That is why a more efficient solution is with the accumulator and the recursive function that will do the addition at the start of the list which is instant!
You can read a bit more about the go
pattern in here: https://kowainik.github.io/posts/haskell-mini-patterns#recursive-go
First of all, thank you for the hard work and thoughtfulness given to create this course, it has really helped me get my heads around Haskell.
It would make my day if you could suggest me some areas of improvements or some suggestions to hone skills in Haskell and Functional Programming
Regards
Solutions for Chapter {1}
cc @vrom911 @chshersh