Skip to content
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

Solutions for Chapter 1,2 & 3 #556

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open

Solutions for Chapter 1,2 & 3 #556

wants to merge 36 commits into from

Conversation

WolfSoko
Copy link

@WolfSoko WolfSoko commented Feb 24, 2023

Solutions for Chapter 1 and 2

cc @vrom911

@WolfSoko WolfSoko changed the title Solutions for Chapter 1 Solutions for Chapter 1 & 2 Feb 25, 2023
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.

Congrats with finishing these chapters 🎉

@@ -520,7 +519,7 @@ branches because it is an expression and it must always return some value.
satisfying the check will be returned and, therefore, evaluated.
-}
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
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

src/Chapter1.hs Show resolved Hide resolved
sumLast2 n = error "sumLast2: Not implemented!"

sumLast2 :: Int -> Int
sumLast2 n = (lastDigit n) + secondLastDigit
Copy link
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

| absN < 10 = absN
| otherwise = firstDigit (div absN 10)
where
absN = abs n
Copy link
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 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
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 🙂

@@ -728,7 +740,7 @@ value of the element itself
🕯 HINT: Use combination of 'map' and 'replicate'
-}
smartReplicate :: [Int] -> [Int]
smartReplicate l = error "smartReplicate: Not implemented!"
smartReplicate l = concat (map (\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.

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 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
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!

src/Chapter2.hs Show resolved Hide resolved
@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