Skip to content
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

Chapter 2 done (and 1) #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Chapter 2 done (and 1)
  • Loading branch information
Linouth committed Aug 6, 2024
commit e12818a850f223ec2526b4e8716d494b210d92cc
76 changes: 48 additions & 28 deletions src/Chapter1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand All @@ -449,7 +450,9 @@ 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 >= 0 = x + 1
| otherwise = x - 1

{- |
After you've implemented the function (or even during the implementation), you
Expand Down Expand Up @@ -490,7 +493,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


{- |
Expand Down Expand Up @@ -520,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)


{- |
Expand Down Expand Up @@ -554,7 +558,13 @@ 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
| y < x && x < z = x
| z < x && x < y = x
| x < y && y < z = y
| z < y && y < x = y
| otherwise = z

{- |
=⚔️= Task 8
Expand All @@ -568,7 +578,10 @@ True
>>> isVowel 'x'
False
-}
isVowel c = error "isVowel: not implemented!"
isVowel :: Char -> Bool
isVowel c
| c `elem` ['a', 'e', 'i', 'o', 'u'] = True
| otherwise = False


{- |
Expand Down Expand Up @@ -632,7 +645,11 @@ 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 = (+) (m n) (m a)
where
m x = mod x 10
a = div n 10


{- |
Expand All @@ -653,7 +670,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 = if x == 0 then n else firstDigit x
where
x = div n 10


{-
Expand Down
Loading
Loading