Skip to content

Solutions for Chapter 1,2 & 3#556

Open
WolfSoko wants to merge 36 commits intokowainik:mainfrom
WolfSoko:main
Open

Solutions for Chapter 1,2 & 3#556
WolfSoko wants to merge 36 commits intokowainik:mainfrom
WolfSoko:main

Conversation

@WolfSoko
Copy link
Copy Markdown

@WolfSoko WolfSoko commented Feb 24, 2023

Solutions for Chapter 1 and 2

cc @vrom911

@WolfSoko WolfSoko requested a review from vrom911 as a code owner February 24, 2023 09:14
@WolfSoko WolfSoko changed the title Solutions for Chapter 1 Solutions for Chapter 1 & 2 Feb 25, 2023
Copy link
Copy Markdown
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.

Congrats with finishing these chapters 🎉

Comment thread src/Chapter1.hs
-}
closestToZero :: Int -> Int -> Int
closestToZero x y = error "closestToZero: not implemented!"
closestToZero x y = if (abs x) < (abs y) then x else y
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

() are redundant here. I see how it can be confusing to see when to use them and when not to use, especially in the beginning. But it's usually better to remove redundant brackets for cleaner code 🧹

Suggested change
closestToZero x y = if (abs x) < (abs y) then x else y
closestToZero x y = if abs x < abs y then x else y

Comment thread src/Chapter1.hs
Comment thread src/Chapter1.hs
sumLast2 n = error "sumLast2: Not implemented!"

sumLast2 :: Int -> Int
sumLast2 n = (lastDigit n) + secondLastDigit
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice reusage of previous lastDigit function! 👏🏼

I see that you have already a var to represent this bit – lastD, you can reuse it here too!

Suggested change
sumLast2 n = (lastDigit n) + secondLastDigit
sumLast2 n = lastD + secondLastDigit

Comment thread src/Chapter1.hs
| absN < 10 = absN
| otherwise = firstDigit (div absN 10)
where
absN = abs n
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very elegant to use where clause here 👍🏼

Comment thread src/Chapter2.hs
Comment on lines +631 to +636
takeEven l = go True l
where
go :: Bool -> [a] -> [a]
go _ [] = []
go True (x:xs) = x : go False xs
go False (_:xs) = go True xs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice idea on the helper functions 👍

Alternatively, you could use pattern matching in takeEven instead. As you need to take every second element, it is possible to represent with a few pattern matching clauses 🙂

Comment thread src/Chapter2.hs
-}
smartReplicate :: [Int] -> [Int]
smartReplicate l = error "smartReplicate: Not implemented!"
smartReplicate l = concat (map (\x -> replicate x x) l)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great! 👍🏼
If you want to write this function even more elegantly, you can use the standard function concatMap which is a combination of concat and map as the name suggests 🙂
After that, you could notice, that you would be able to eta-reduce on the last argument :)

Comment thread src/Chapter2.hs
Comment on lines +758 to +762
go :: Int -> [[Int]] -> [[Int]]
go _ [] = []
go n (x:xs) = if n `elem` x
then x : go n xs
else go n xs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice go! It is actually the standard filter function, that you can use instead of the custom go, but it is correct!

Comment thread src/Chapter2.hs
@WolfSoko WolfSoko changed the title Solutions for Chapter 1 & 2 Solutions for Chapter 1,2 & 3 Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants