-
-
Notifications
You must be signed in to change notification settings - Fork 848
Completed Chapter 1, 2, 3 and 4 #583
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,31 +209,31 @@ So, the output in this example means that 'False' has type 'Bool'. | |
| > Try to guess first and then compare your expectations with GHCi output | ||
|
|
||
| >>> :t True | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| True :: Bool | ||
| >>> :t 'a' | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 'a' :: Char | ||
| >>> :t 42 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 42 :: Num a => a | ||
|
|
||
| A pair of boolean and char: | ||
| >>> :t (True, 'x') | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| (True, 'x') :: (Bool, Char) | ||
|
|
||
| Boolean negation: | ||
| >>> :t not | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| not :: Bool -> Bool | ||
|
|
||
| Boolean 'and' operator: | ||
| >>> :t (&&) | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| (&&) :: Bool -> Bool -> Bool | ||
|
|
||
| Addition of two numbers: | ||
| >>> :t (+) | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| (+) :: Num a => a -> a -> a | ||
|
|
||
| Maximum of two values: | ||
| >>> :t max | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| max :: Ord a => a -> a -> a | ||
|
|
||
| You might not understand each type at this moment, but don't worry! You've only | ||
| started your Haskell journey. Types will become your friends soon. | ||
|
|
@@ -301,43 +301,43 @@ expressions in GHCi | |
| functions and operators first. Remember this from the previous task? ;) | ||
|
|
||
| >>> 1 + 2 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 3 | ||
|
|
||
| >>> 10 - 15 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 5 | ||
|
|
||
| >>> 10 - (-5) -- negative constants require () | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 15 | ||
|
|
||
| >>> (3 + 5) < 10 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| True | ||
|
|
||
| >>> True && False | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| False | ||
|
|
||
| >>> 10 < 20 || 20 < 5 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| True | ||
|
|
||
| >>> 2 ^ 10 -- power | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 1024 | ||
|
|
||
| >>> not False | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| True | ||
|
|
||
| >>> div 20 3 -- integral division | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 6 | ||
|
|
||
| >>> mod 20 3 -- integral division remainder | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 2 | ||
|
|
||
| >>> max 4 10 | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 10 | ||
|
|
||
| >>> min 5 (max 1 2) | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 2 | ||
|
|
||
| >>> max (min 1 10) (min 5 7) | ||
| <INSERT THE RESULT INSTEAD OF THE TEXT> | ||
| 5 | ||
|
|
||
| Because Haskell is a __statically-typed__ language, you see an error each time | ||
| you try to mix values of different types in situations where you are not | ||
|
|
@@ -429,9 +429,13 @@ task is to specify the type of this function. | |
| 49 | ||
| -} | ||
|
|
||
| squareSum :: Num a => a -> a -> a | ||
| squareSum x y = (x + y) * (x + y) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| {- | | ||
| =⚔️= Task 4 | ||
|
|
||
|
|
@@ -448,8 +452,8 @@ Implement the function that takes an integer value and returns the next 'Int'. | |
| every type 。.☆.*。. No need to worry much about "error" here, just replace the | ||
| function body with the proper implementation. | ||
| -} | ||
| next :: Int -> Int | ||
| next x = error "next: not implemented!" | ||
| next :: Num a => a -> a | ||
| next x = x + 1 | ||
|
|
||
| {- | | ||
| After you've implemented the function (or even during the implementation), you | ||
|
|
@@ -490,7 +494,8 @@ Implement a function that returns the last digit of a given number. | |
| whether it works for you! | ||
| -} | ||
| -- DON'T FORGET TO SPECIFY THE TYPE IN HERE | ||
| lastDigit n = error "lastDigit: Not implemented!" | ||
| lastDigit :: Integral a => a -> a | ||
| lastDigit n = n `mod` 10 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your implementation is almost correct 🆗 |
||
|
|
||
|
|
||
| {- | | ||
|
|
@@ -519,8 +524,8 @@ branches because it is an expression and it must always return some value. | |
| 👩🔬 Due to lazy evaluation in Haskell, only the expression from the branch | ||
| satisfying the check will be returned and, therefore, evaluated. | ||
| -} | ||
| closestToZero :: Int -> Int -> Int | ||
| closestToZero x y = error "closestToZero: not implemented!" | ||
| closestToZero :: (Ord a, Num a) => a -> a -> a | ||
| closestToZero x y = if abs x < abs y then x else y | ||
|
|
||
|
|
||
| {- | | ||
|
|
@@ -554,7 +559,21 @@ value after "=" where the condition is true. | |
| Casual reminder about adding top-level type signatures for all functions :) | ||
| -} | ||
|
|
||
| mid x y z = error "mid: not implemented!" | ||
| mid :: Ord a => a -> a -> a -> a | ||
| mid x y z | ||
| | (x <= y && y <= z) || (z <= y && y <= x) = y | ||
| | (y <= x && x <= z) || (z <= x && x <= y) = x | ||
| | otherwise = z | ||
|
|
||
| -- mid x y z = x + y + z - minimum [x, y, z] - maximum [x, y, z] | ||
|
|
||
|
|
||
| -- mid x y z | ||
| -- | x > y = mid y x z | ||
| -- | y > z = mid x z y | ||
| -- | otherwise = y | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really smashed it! Nice ideas all around 💡 |
||
|
|
||
|
|
||
|
|
||
| {- | | ||
| =⚔️= Task 8 | ||
|
|
@@ -568,7 +587,9 @@ True | |
| >>> isVowel 'x' | ||
| False | ||
| -} | ||
| isVowel c = error "isVowel: not implemented!" | ||
| isVowel :: Char -> Bool | ||
| isVowel c = c `elem` "aeiouy" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice one! 👍🏼 One note in here, that sometimes, |
||
|
|
||
|
|
||
|
|
||
| {- | | ||
|
|
@@ -632,7 +653,16 @@ Try to introduce variables in this task (either with let-in or where) to avoid | |
| specifying complex expressions. | ||
| -} | ||
|
|
||
| sumLast2 n = error "sumLast2: Not implemented!" | ||
| sumLast2 :: Integral a => a -> a | ||
| sumLast2 n = | ||
| let lastDigit = n `mod` 10 | ||
| secondLastDigit = (n `div` 10) `mod` 10 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a wonderful solution! 👏🏼 You correctly noticed that it is the One hint to make your solution even shorter: you can see that you use both: mod m 10
div m 10The standard library has the So you could write it this way: (x, y) = divMod m 10You can see how we could pattern match on the pair 🙂 |
||
| in lastDigit + secondLastDigit | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| {- | | ||
|
|
@@ -653,7 +683,10 @@ You need to use recursion in this task. Feel free to return to it later, if you | |
| aren't ready for this boss yet! | ||
| -} | ||
|
|
||
| firstDigit n = error "firstDigit: Not implemented!" | ||
| firstDigit :: Integral t => t -> t | ||
| firstDigit n | ||
| | n < 10 = n | ||
| | otherwise = firstDigit (n `div` 10) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that for a negative number like -19, this implementation will return the negative number itself instead of the first digit because the first check n < 10 will always succeed for negative numbers. |
||
|
|
||
|
|
||
| {- | ||
|
|
@@ -668,3 +701,4 @@ Modules: http://learnyouahaskell.com/modules | |
| Let vs where: https://wiki.haskell.org/Let_vs._Where | ||
| Packages and modules in Haskell: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/packages.html | ||
| -} | ||
|
|
||
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.
We explain
Numin the following chapters, so you are one step ahead 😸In here
Int -> Int -> Intwould also work, as a more specific type 🙂