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

completed Chapter 2 #538

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

Conversation

kumarpriyanshu2k2
Copy link

Solutions for Chapter 2

cc @vrom911 @chshersh

@vrom911 vrom911 added hacktoberfest-accepted https://hacktoberfest.digitalocean.com/ chapter2 labels Oct 7, 2022
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.

Great onE 🏆

src/Chapter2.hs Outdated Show resolved Hide resolved
@@ -336,8 +338,10 @@ from it!
ghci> :l src/Chapter2.hs
-}
subList :: Int -> Int -> [a] -> [a]
subList = error "subList: Not implemented!"

subList x y z = if y<x || y>length z then [] else
Copy link
Member

Choose a reason for hiding this comment

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

It is more Haskell-way to separate all operators from the arguments with spaces 👾

Suggested change
subList x y z = if y<x || y>length z then [] else
subList x y z = if y < x || y > length z then [] else

src/Chapter2.hs Outdated Show resolved Hide resolved
Comment on lines +756 to +757
contains _ []=[]
contains y (x:xs) = if elem y x then [x] ++ contains y xs else []++ contains y xs
Copy link
Member

Choose a reason for hiding this comment

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

You can use filter function here instead of the pattern matching, but this is nice solution already (you'll just need to replace ++ with : operator 🙂 )


-- 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 🏆

rotate = error "rotate: Not implemented!"
rotate :: Int -> [a] -> [a]
rotate x s =
let y = cycle s
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, cycle fails in runtime on empty lists ♻️ So you need to handle this case separately for this function to work properly.

rewind = error "rewind: Not Implemented!"
rewind :: [a] -> [a]
rewind [] = []
rewind(x:xs) = rewind xs ++ [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

kumarpriyanshu2k2 and others added 2 commits October 9, 2022 21:43
Co-authored-by: Veronika Romashkina <vrom911@gmail.com>
Co-authored-by: Veronika Romashkina <vrom911@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chapter2 hacktoberfest-accepted https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants