diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 406deeaca..7b3513736 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -209,31 +209,33 @@ 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 - +True :: Bool + >>> :t 'a' - +'a' :: Char + >>> :t 42 - +42 :: Num a => a A pair of boolean and char: >>> :t (True, 'x') - +(True, 'x') :: (Bool, Char) Boolean negation: >>> :t not - +not :: Bool -> Bool Boolean 'and' operator: >>> :t (&&) - +(&&) :: Bool -> Bool -> Bool Addition of two numbers: >>> :t (+) - +(+) :: Num a => a -> a -> a Maximum of two values: >>> :t max - +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,19 +303,19 @@ expressions in GHCi functions and operators first. Remember this from the previous task? ;) >>> 1 + 2 - +3 >>> 10 - 15 - +-5 >>> 10 - (-5) -- negative constants require () - +15 >>> (3 + 5) < 10 - +True >>> True && False - +False >>> 10 < 20 || 20 < 5 @@ -322,22 +324,22 @@ expressions in GHCi >>> not False - +True >>> div 20 3 -- integral division - +6 >>> mod 20 3 -- integral division remainder - +2 >>> max 4 10 - +10 >>> min 5 (max 1 2) - +2 >>> max (min 1 10) (min 5 7) - +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,6 +431,7 @@ task is to specify the type of this function. 49 -} +squareSum :: Int -> Int -> Int squareSum x y = (x + y) * (x + y) @@ -448,8 +451,13 @@ 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 :: Int -> Int +-- next = succ next :: Int -> Int -next x = error "next: not implemented!" +next = (+1) {- | After you've implemented the function (or even during the implementation), you @@ -490,8 +498,10 @@ 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 n = error "lastDigit: Not implemented!" +lastDigit :: Int -> Int +lastDigit x = mod x 10 {- | =⚔️= Task 6 @@ -519,9 +529,18 @@ 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 :: Int -> Int -> Int +-- closestToZero x y = error "closestToZero: not implemented!" +-- closestToZero x y | xAbs <= yAbs = xAbs +-- | yAbs <= xAbs = yAbs +-- where xAbs = abs x +-- yAbs = abs y + +closestToZero :: Int -> Int -> Int +closestToZero x y = min xAbs yAbs + where xAbs = abs x + yAbs = abs y {- | =⚔️= Task 7 @@ -554,7 +573,17 @@ 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 x y z = error "mid: not implemented!" + +mid :: Int -> Int -> Int -> Int +mid x y z | x <= y && y <= z = y + | x <= z && z <= y = z + | y <= x && x <= z = x + | y <= z && z <= x = z + | z <= x && x <= y = x + | z <= y && y <= x = y + | otherwise = x + {- | =⚔️= Task 8 @@ -568,8 +597,11 @@ True >>> isVowel 'x' False -} -isVowel c = error "isVowel: not implemented!" +-- isVowel c = error "isVowel: not implemented!" +isVowel :: Char -> Bool +isVowel n | n == 'a' || n == 'e' || n == 'i' || n == 'o' || n == 'u' = True + | otherwise = False {- | == Local variables and functions @@ -632,9 +664,13 @@ 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 n = error "sumLast2: Not implemented!" +sumLast2 :: Int -> Int +sumLast2 n = let lastTwoDigits = n `mod` 100 + lastFirst = lastTwoDigits `mod` 10 + lastSecond = (lastTwoDigits - lastFirst) `div` 10 + in lastFirst + lastSecond {- | =💣= Task 10* @@ -653,8 +689,11 @@ 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 n = error "firstDigit: Not implemented!" +firstDigit :: Int -> Int +firstDigit n | n < 10 = n +firstDigit n = firstDigit (n `div` 10) {- You did it! Now it is time to open a pull request with your changes