-
-
Notifications
You must be signed in to change notification settings - Fork 848
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
Complete chapter 2 #534
base: main
Are you sure you want to change the base?
Complete chapter 2 #534
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.
Nice one!! Very good job 💪🏼
@@ -336,7 +347,8 @@ from it! | |||
ghci> :l src/Chapter2.hs | |||
-} | |||
subList :: Int -> Int -> [a] -> [a] | |||
subList = error "subList: Not implemented!" | |||
subList a b xs = if a > b 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.
Your implementation is correct when the right bound is lower than the left. But I think you also check on negative bounds here too 👌🏼
|
||
firstHalf :: [a] -> [a] | ||
firstHalf l = take len l | ||
where len = (length l) `div` 2 |
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.
Neat usage of where
! Note that brackets are not needed when you use the function in the infix form 🙂
where len = (length l) `div` 2 | |
where len = length l `div` 2 |
isThird42 = error "isThird42: Not implemented!" | ||
|
||
isThird42 :: Integral a => [a] -> Bool | ||
isThird42 (_ : _ : x : _) = x == 42 |
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 don't need to compare x
with 42 separately, you can pattern match on 42
directly in the pattern.
isThird42 (_ : _ : x : _) = x == 42 | |
isThird42 (_ : _ : 42 : _) = True |
smartReplicate :: [Int] -> [Int] | ||
smartReplicate l = error "smartReplicate: Not implemented!" | ||
smartReplicate l = concatMap (\x -> replicate x x) l |
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 :: Num a => [a] -> [a] -> [a] | ||
pairMul xs = (zipWith (*) 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 go even further and eta reduce xs
as well:
pairMul xs = (zipWith (*) xs) | |
pairMul = zipWith (*) |
@@ -842,7 +867,9 @@ list. | |||
|
|||
🕯 HINT: Use the 'cycle' function | |||
-} | |||
rotate = error "rotate: Not implemented!" | |||
rotate n xs = if (n /= (abs n)) then |
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.
Is this a check on negative number?
In that case, you can be more explicit about it, I think 🙂
rotate = error "rotate: Not implemented!" | ||
rotate n xs = if (n /= (abs n)) then | ||
[] | ||
else take (length xs) $ drop n $ (cycle 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.
Unfortunately, cycle
fails in runtime on empty lists ♻️ So you need to handle this case separately for this function to work properly.
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.
Also, you can optimise the solution by dropping only mod n (length of the list)
🚤
|
||
rewind :: [a] -> [a] | ||
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
Solutions for Chapter 2
cc @vrom911 @chshersh