Skip to content

Lecture 3 solutions #78

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 41 additions & 12 deletions src/Lecture1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ module Lecture1
, lowerAndGreater
) where

-- VVV If you need to import libraries, do it after this line ... VVV

-- ^^^ and before this line. Otherwise the test suite might fail ^^^

{- | Specify the type signature of the following function. Think about
its behaviour, possible types for the function arguments and write the
type signature explicitly.
-}
makeSnippet :: Int -> [Char] -> [Char]
makeSnippet limit text = take limit ("Description: " ++ text) ++ "..."

{- | Implement a function that takes two numbers and finds sum of
Expand All @@ -53,8 +50,8 @@ their squares.
Explanation: @sumOfSquares 3 4@ should be equal to @9 + 16@ and this
is 25.
-}
-- DON'T FORGET TO SPECIFY THE TYPE IN HERE
sumOfSquares x y = error "TODO!"
sumOfSquares :: Int -> Int -> Int
sumOfSquares x y = x * x + y * y

{- | Implement a function that returns the last digit of a given number.

Expand All @@ -66,8 +63,8 @@ sumOfSquares x y = error "TODO!"
🕯 HINT: use the @mod@ function

-}
-- DON'T FORGET TO SPECIFY THE TYPE IN HERE
lastDigit n = error "lastDigit: Not implemented!"
lastDigit :: Int -> Int
lastDigit n = mod (abs n) 10

{- | Write a function that takes three numbers and returns the
difference between the biggest number and the smallest one.
Expand All @@ -81,7 +78,11 @@ and 1 is the smallest, and 7 - 1 = 6.
Try to use local variables (either let-in or where) to implement this
function.
-}
minmax x y z = error "TODO"
minmax :: Int -> Int -> Int -> Int
minmax x y z =
let maxVal = max x (max y z)
minVal = min x (min y z)
in maxVal - minVal

{- | Implement a function that takes a string, start and end positions
and returns a substring of a given string from the start position to
Expand All @@ -98,7 +99,16 @@ start position can be considered as zero (e.g. substring from the
first character) and negative end position should result in an empty
string.
-}
subString start end str = error "TODO"
subString :: Int -> Int -> [Char] -> [Char]
subString start end str
| end < 0 = ""
| start < 0 = take (end + 1) str
| otherwise = take (end - start + 1) (drop start str)

{- Alternative one-line implementation:

subString start end str = take (max (-1) end - max 0 start + 1) (drop start str)
-}

{- | Write a function that takes a String — space separated numbers,
and finds a sum of the numbers inside this string.
Expand All @@ -108,7 +118,8 @@ and finds a sum of the numbers inside this string.

The string contains only spaces and/or numbers.
-}
strSum str = error "TODO"
strSum :: [Char] -> Int
strSum str = sum (map read (words str))

{- | Write a function that takes a number and a list of numbers and
returns a string, saying how many elements of the list are strictly
Expand All @@ -123,4 +134,22 @@ and lower than 6 elements (4, 5, 6, 7, 8 and 9).

🕯 HINT: Use recursion to implement this function.
-}
lowerAndGreater n list = error "TODO"
lowerAndGreater :: Int -> [Int] -> [Char]
lowerAndGreater n list = go 0 0 list
where
go :: Int -> Int -> [Int] -> [Char]
go lower greater l
| null l = display lower greater
| head l > n = go (lower + 1) greater (tail l)
| head l < n = go lower (greater + 1) (tail l)
| otherwise = go lower greater (tail l)

display :: Int -> Int -> [Char]
display lower greater =
show n
++ " is greater than "
++ show greater
++ " elements and lower than "
++ show lower
++ " elements"

13 changes: 5 additions & 8 deletions src/Lecture2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ module Lecture2
, constantFolding
) where

-- VVV If you need to import libraries, do it after this line ... VVV

-- ^^^ and before this line. Otherwise the test suite might fail ^^^

{- | Implement a function that finds a product of all the numbers in
the list. But implement a lazier version of this function: if you see
zero, you can stop calculating product and return 0 immediately.
Expand Down Expand Up @@ -167,12 +163,12 @@ data Knight = Knight
dragonFight = error "TODO"

----------------------------------------------------------------------------
-- Extra Challenges
-- Challenges
----------------------------------------------------------------------------

{- The following exercises are considered optional. Some of them might be more
challenging. However, you still may find some of them easier than some of the
previous ones. Difficulty is a relative concept.
{- The following exercises are considered more challenging. However,
you still may find some of them easier than some of the previous
ones. Difficulty is a relative concept.
-}

{- | Write a function that takes a list of numbers and returns 'True'
Expand Down Expand Up @@ -293,3 +289,4 @@ Folding" optimization on the given expression.
-}
constantFolding :: Expr -> Expr
constantFolding = error "TODO"

4 changes: 1 addition & 3 deletions src/Lecture3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ module Lecture3
, apply
) where

-- VVV If you need to import libraries, do it after this line ... VVV

-- ^^^ and before this line. Otherwise the test suite might fail ^^^

-- $setup
-- >>> import Data.Semigroup
Expand Down Expand Up @@ -250,3 +247,4 @@ Just [8,9,10]

-}
apply = error "TODO"