From c4310e9b97e0a0595e68e57705c344dbff76fd7d Mon Sep 17 00:00:00 2001 From: Matthias Holzinger Date: Sun, 1 Dec 2024 13:23:01 +0100 Subject: [PATCH 1/2] feat: solve chapter 1 --- src/Chapter1.hs | 74 ++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/Chapter1.hs b/src/Chapter1.hs index 406deeaca..51a3588d0 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 - +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,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 @@ -428,7 +428,7 @@ task is to specify the type of this function. >>> squareSum 3 4 49 -} - +squareSum :: Int -> Int -> Int squareSum x y = (x + y) * (x + y) @@ -449,7 +449,7 @@ Implement the function that takes an integer value and returns the next 'Int'. 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 @@ -489,8 +489,8 @@ Implement a function that returns the last digit of a given number. results. Or you can try to guess the function name, search for it and check 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 (abs n) 10 {- | @@ -520,7 +520,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 = if (min (abs x) (abs y) == (abs x)) then x else y {- | @@ -554,7 +554,11 @@ 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 + | (x > min y z && x < max y z) || x == y || x == z = x + | (z > min x y && z < max x y) || z == y = z + | otherwise = y {- | =⚔️= Task 8 @@ -568,7 +572,8 @@ True >>> isVowel 'x' False -} -isVowel c = error "isVowel: not implemented!" +isVowel :: Char -> Bool +isVowel c = elem c ['a', 'e', 'i', 'o', 'u'] {- | @@ -632,8 +637,10 @@ 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 beforeLastDigit = lastDigit (div (abs n) 10) + in (lastDigit n) + beforeLastDigit {- | =💣= Task 10* @@ -653,8 +660,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 :: Int -> Int +firstDigit n + | i < 10 = i + | otherwise = firstDigit (div i 10) + where i = abs n {- You did it! Now it is time to open a pull request with your changes From f4da48ddc90926e6e3618973d64fe7081c905ce3 Mon Sep 17 00:00:00 2001 From: Matthias 'Yolgie' Holzinger Date: Tue, 3 Dec 2024 14:41:18 +0100 Subject: [PATCH 2/2] Update to haskell-actions/setup in ci.yml --- .github/workflows/ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 189685c79..b1e4cb30f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: haskell/actions/setup@v2 + - uses: haskell-actions/setup@v2.7.6 name: Setup Haskell with: ghc-version: ${{ matrix.ghc }} @@ -45,7 +45,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: haskell/actions/setup@v2 + - uses: haskell-actions/setup@v2.7.6 name: Setup Haskell with: ghc-version: ${{ matrix.ghc }} @@ -80,7 +80,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: haskell/actions/setup@v2 + - uses: haskell-actions/setup@v2.7.6 name: Setup Haskell with: ghc-version: ${{ matrix.ghc }} @@ -115,7 +115,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: haskell/actions/setup@v2 + - uses: haskell-actions/setup@v2.7.6 name: Setup Haskell with: ghc-version: ${{ matrix.ghc }} @@ -141,7 +141,7 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: haskell/actions/setup@v2 + - uses: haskell-actions/setup@v2.7.6 name: Setup Haskell with: ghc-version: ${{ matrix.ghc }}