Skip to content

Commit

Permalink
[+] Chapter1
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Oct 1, 2020
1 parent 785bc85 commit 55c772f
Showing 1 changed file with 58 additions and 34 deletions.
92 changes: 58 additions & 34 deletions src/Chapter1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,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 p => p
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 @@ -296,31 +296,44 @@ expressions in GHCi
functions and operators first. Remember this from the previous task? ;)
>>> 1 + 2
INSERT THE RESULT INSTEAD OF THE TEXT
3
it :: Num a => a
>>> 10 - 15
-5
it :: Num a => a
>>> 10 - (-5) -- negative constants require ()
15
it :: Num a => a
>>> (3 + 5) < 10
True
it :: Bool
>>> True && False
False
it :: Bool
>>> 10 < 20 || 20 < 5
True
it :: Bool
>>> 2 ^ 10 -- power
1024
it :: Num a => a
>>> not False
True
it :: Bool
>>> div 20 3 -- integral division
6
it :: Integral a => a
>>> mod 20 3 -- integral division remainder
2
it :: Integral a => a
>>> max 4 10
10
it :: (Ord a, Num a) => a
>>> min 5 (max 1 2)
2
it :: (Ord a, Num a) => a
>>> max (min 1 10) (min 5 7)
5
it :: (Ord a, Num a) => a
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 @@ -411,7 +424,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)


Expand All @@ -432,7 +445,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
Expand Down Expand Up @@ -472,8 +485,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 n 10


{- |
Expand Down Expand Up @@ -503,7 +516,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 @@ -536,8 +549,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 = max (min x y) (min (max x y) z)

{- |
=⚔️= Task 8
Expand All @@ -551,8 +564,13 @@ True
>>> isVowel 'x'
False
-}
isVowel c = error "isVowel: not implemented!"

isVowel c
| c == 'a' = True
| c == 'e' = True
| c == 'i' = True
| c == 'o' = True
| c == 'u' = True
| otherwise = False

{- |
== Local variables and functions
Expand Down Expand Up @@ -615,8 +633,12 @@ 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 = first n + second n
where
first :: Int -> Int
first n = mod n 10
second :: Int -> Int
second n = mod (div n 10) 10

{- |
=💣= Task 10*
Expand All @@ -635,8 +657,10 @@ Implement a function that returns the first digit of a given number.
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
| n < 10 = n
| otherwise = firstDigit $ div n 10


{-
Expand Down

0 comments on commit 55c772f

Please sign in to comment.