From ee10bbc3fdc46936c28b128e06bcb3777eaf3d0f Mon Sep 17 00:00:00 2001 From: "G. Reiter" Date: Fri, 14 Oct 2022 00:12:29 +0200 Subject: [PATCH 1/6] Part 1 for Chapter 1 --- src/Chapter1.hs | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 04fed0db..017cb63e 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -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 - +False :: 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,43 +301,43 @@ 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 - +True >>> 2 ^ 10 -- power - +1024 >>> 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 +429,7 @@ task is to specify the type of this function. 49 -} +squareSum :: Int -> Int -> Int squareSum x y = (x + y) * (x + y) @@ -448,8 +449,9 @@ 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 x = x + 1 {- | After you've implemented the function (or even during the implementation), you @@ -490,7 +492,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 :: Int -> Int +lastDigit n = mod n 10 {- | From 51b36ed4ee2d4792ba94cd1a90f3d2141d6c548a Mon Sep 17 00:00:00 2001 From: Guntbert Reiter Date: Fri, 14 Oct 2022 21:48:12 +0200 Subject: [PATCH 2/6] Finished Chapter1 --- src/Chapter1.hs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 017cb63e..be16d318 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -72,6 +72,7 @@ the `.hs` extension. -} module Chapter1 where +import Data.List {- | In Haskell, we have __expressions__. Expressions can be represented by some primitive values (numbers: 1, 100; characters: 'a', 'z'; booleans: True, False; @@ -209,7 +210,7 @@ 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 -False :: Bool +True :: Bool >>> :t 'a' 'a' :: Char >>> :t 42 @@ -523,7 +524,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 = min (abs x) (abs y) {- | @@ -556,8 +557,8 @@ 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 :: Int -> Int -> Int -> Int +mid x y z = head (tail(sort[x,y,z])) {- | =⚔️= Task 8 @@ -571,7 +572,10 @@ True >>> isVowel 'x' False -} -isVowel c = error "isVowel: not implemented!" +isVowel :: Char -> Bool +isVowel c + | elem c ['a', 'e', 'i', 'o', 'u'] = True + | otherwise = False {- | @@ -634,8 +638,11 @@ Implement a function that returns the sum of the last two digits of a number. 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 :: Int -> Int +sumLast2 n = + let digit1 = mod n 10 + digit2 = mod (div n 10) 10 + in digit1 + digit2 {- | @@ -656,7 +663,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 :: Int -> Int +firstDigit n + | abs n < 10 = n + | otherwise = firstDigit (div n 10) {- From d2df71a47cb2a03a766904ff4928df3857751340 Mon Sep 17 00:00:00 2001 From: "G. Reiter" Date: Fri, 14 Oct 2022 23:01:05 +0200 Subject: [PATCH 3/6] Kleinigkeiten... --- src/Chapter1.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index be16d318..2e798cf0 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -72,7 +72,7 @@ the `.hs` extension. -} module Chapter1 where -import Data.List +import Data.List (sort) {- | In Haskell, we have __expressions__. Expressions can be represented by some primitive values (numbers: 1, 100; characters: 'a', 'z'; booleans: True, False; @@ -214,7 +214,7 @@ True :: Bool >>> :t 'a' 'a' :: Char >>> :t 42 -42 :: Num a => a +42 :: Num p => p A pair of boolean and char: >>> :t (True, 'x') From d12851b49ea973164cc7fc1b07f890284a96d430 Mon Sep 17 00:00:00 2001 From: "G. Reiter" Date: Fri, 14 Oct 2022 23:12:23 +0200 Subject: [PATCH 4/6] Handle negative numbers correctly --- src/Chapter1.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 2e798cf0..482b9cbe 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -494,7 +494,7 @@ Implement a function that returns the last digit of a given number. -} -- DON'T FORGET TO SPECIFY THE TYPE IN HERE lastDigit :: Int -> Int -lastDigit n = mod n 10 +lastDigit n = mod (abs n) 10 {- | @@ -524,7 +524,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 = min (abs x) (abs y) +closestToZero x y = if (abs x) <= (abs y) then x else y {- | @@ -640,8 +640,8 @@ specifying complex expressions. -} sumLast2 :: Int -> Int sumLast2 n = - let digit1 = mod n 10 - digit2 = mod (div n 10) 10 + let digit1 = mod (abs n) 10 + digit2 = mod (div (abs n) 10) 10 in digit1 + digit2 From f632b620b44d65140613e0cec7d9b5856980d0ca Mon Sep 17 00:00:00 2001 From: Guntbert Reiter Date: Thu, 27 Oct 2022 21:36:17 +0200 Subject: [PATCH 5/6] Omit redundant brackets Co-authored-by: Veronika Romashkina --- src/Chapter1.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 482b9cbe..63e48f28 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -524,7 +524,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 = if (abs x) <= (abs y) then x else y +closestToZero x y = if abs x < abs y then x else y {- | From 4640b1d06c6ac0358006d2accd794dd307992c27 Mon Sep 17 00:00:00 2001 From: Guntbert Reiter Date: Thu, 27 Oct 2022 22:14:25 +0200 Subject: [PATCH 6/6] Fix firstDigit for negative numbers --- src/Chapter1.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 63e48f28..67fc09a4 100644 --- a/src/Chapter1.hs +++ b/src/Chapter1.hs @@ -665,8 +665,8 @@ aren't ready for this boss yet! firstDigit :: Int -> Int firstDigit n - | abs n < 10 = n - | otherwise = firstDigit (div n 10) + | abs n < 10 = abs n + | otherwise = firstDigit (div (abs n) 10) {-