Skip to content

Conversation

@lillycham
Copy link

Solutions for Chapter 2

cc @vrom911 @chshersh

@lillycham lillycham requested a review from vrom911 as a code owner October 5, 2022 13:48
@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.

Nice one!! Very good job 💪🏼

-}
subList :: Int -> Int -> [a] -> [a]
subList = error "subList: Not implemented!"
subList a b xs = if a > b 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.

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

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 🙂

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

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.

Suggested change
isThird42 (_ : _ : x : _) = x == 42
isThird42 (_ : _ : 42 : _) = True


smartReplicate :: [Int] -> [Int]
smartReplicate l = error "smartReplicate: Not implemented!"
smartReplicate l = concatMap (\x -> replicate x x) l
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 :: Num a => [a] -> [a] -> [a]
pairMul xs = (zipWith (*) 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 go even further and eta reduce xs as well:

Suggested change
pairMul xs = (zipWith (*) xs)
pairMul = zipWith (*)

🕯 HINT: Use the 'cycle' function
-}
rotate = error "rotate: Not implemented!"
rotate n xs = if (n /= (abs n)) then
Copy link
Member

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)
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.

Copy link
Member

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 = error "rewind: Not Implemented!"

rewind :: [a] -> [a]
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

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.

2 participants