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

Thank You developers!! #514

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

Thank You developers!! #514

wants to merge 5 commits into from

Conversation

abhideepd
Copy link

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

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.

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

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:

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

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

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:

Suggested change
closestToZero x y = if (abs x < abs y) then x else y
closestToZero x y = if abs x < abs y then x else y

Comment on lines +559 to +561
| y<=(max x z)&&y>=(min x z)=y
| x<=(max z y)&&x>=(min z y)=x
| otherwise =z
Copy link
Member

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:

Suggested change
| 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))
Copy link
Member

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:

Suggested change
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 🙂

Comment on lines +631 to +632
| null x=[]
| otherwise=(head x):(head x):duplicate (tail x)
Copy link
Member

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

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

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 (*)
Copy link
Member

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):[])
Copy link
Member

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

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.

None yet

2 participants