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

solve Lecture1 #1

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

solve Lecture1 #1

wants to merge 2 commits into from

Conversation

racagogi
Copy link
Owner

Solutions for Lecture 1

cc @chshersh

Copy link

@chshersh chshersh left a comment

Choose a reason for hiding this comment

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

Congrats! The first homework is complete 👏🏼
Hope you enjoyed the course so far. Keep up the great work, and feel free to ask any questions if you have 👌🏼

@@ -54,7 +55,8 @@ Explanation: @sumOfSquares 3 4@ should be equal to @9 + 16@ and this
is 25.
-}
-- DON'T FORGET TO SPECIFY THE TYPE IN HERE
sumOfSquares x y = error "TODO!"
sumOfSquares :: Num a => a -> a -> a

Choose a reason for hiding this comment

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

Num is explained in the following lectures, so you are one step ahead 😸
In here Int -> Int -> Int or Integer -> Integer -> Integer would also work, as a more specific type 🙂

src/Lecture1.hs Outdated Show resolved Hide resolved
lastDigit n = error "lastDigit: Not implemented!"
lastDigit :: Integral a => a -> a
lastDigit n =
mod (abs n) 10

Choose a reason for hiding this comment

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

Absolutely correct 👍🏻
Alternatively, you can use the infix form of a function by putting it inside backticks 🙂

Suggested change
mod (abs n) 10
abs n `mod` 10

lowerAndGreater n list =
iter n list 0 0
where
iter n [] lower greater = show n

Choose a reason for hiding this comment

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

I see that the number n that you pass in recursive calls doesn't change. You always pass the same number. In that case, you can remove this extra argument and use n from the function instead. It's possible because functions in where can see all the arguments of the top-level function 🔍

lowerAndGreater n list = error "TODO"
lowerAndGreater :: (Show a, Ord a) => a -> [a] -> [Char]
lowerAndGreater n list =
iter n list 0 0

Choose a reason for hiding this comment

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

Numeric literals in Haskell are polymorphic. That's why you have lots of GHC warnings about defaulting constraints.

To fix this, you can either specify the type of iter explicitly or specialize literals here

Copy link
Owner Author

Choose a reason for hiding this comment

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

thanks for great instuctions.
Actually I use HLS to specify types and formmating.

Here refctoring iter

    iter [] lower greater =
      show n
        ++ " is greater than "
        ++ show lower
        ++ " elements and lower than "
        ++ show greater
        ++ " elements"
    iter (h : t) lower greater
      | h < n = iter  t (lower + 1) greater
      | h > n = iter  t lower (greater + 1)
      | otherwise = iter  t lower greater

Choose a reason for hiding this comment

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

Looks great! And yes, HLS can be really helpful 👏🏻

Co-authored-by: Dmitrii Kovanikov <[email protected]>
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