Conversation
vrom911
left a comment
There was a problem hiding this comment.
Nice one!! Very good job πͺπΌ
| -} | ||
| 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.
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.
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.
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.
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.
You can go even further and eta reduce xs as well:
| pairMul xs = (zipWith (*) xs) | |
| pairMul = zipWith (*) |
| π― 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.
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.
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.
Also, you can optimise the solution by dropping only mod n (length of the list) π€
| rewind = error "rewind: Not Implemented!" | ||
|
|
||
| rewind :: [a] -> [a] | ||
| rewind (x:xs) = rewind xs ++ [x] |
There was a problem hiding this comment.
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