-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 🙂
lastDigit n = error "lastDigit: Not implemented!" | ||
lastDigit :: Integral a => a -> a | ||
lastDigit n = | ||
mod (abs n) 10 |
There was a problem hiding this comment.
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 🙂
mod (abs n) 10 | |
abs n `mod` 10 |
lowerAndGreater n list = | ||
iter n list 0 0 | ||
where | ||
iter n [] lower greater = show n |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]>
Solutions for Lecture 1
cc @chshersh