-
-
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
completed Chapter 2 #538
base: main
Are you sure you want to change the base?
completed Chapter 2 #538
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.
Great onE 🏆
@@ -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 |
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.
It is more Haskell-way to separate all operators from the arguments with spaces 👾
subList x y z = if y<x || y>length z then [] else | |
subList x y z = if y < x || y > length z then [] else |
contains _ []=[] | ||
contains y (x:xs) = if elem y x then [x] ++ contains y xs else []++ contains y xs |
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.
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 (*) |
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 🏆
rotate = error "rotate: Not implemented!" | ||
rotate :: Int -> [a] -> [a] | ||
rotate x s = | ||
let y = cycle s |
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.
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] |
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
Co-authored-by: Veronika Romashkina <[email protected]>
Co-authored-by: Veronika Romashkina <[email protected]>
Solutions for Chapter 2
cc @vrom911 @chshersh