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

Update Lecture1.hs #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions src/Lecture1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Lecture1
its behaviour, possible types for the function arguments and write the
type signature explicitly.
-}
makeSnippet:: String -> String
makeSnippet limit text = take limit ("Description: " ++ text) ++ "..."

{- | Implement a function that takes two numbers and finds sum of
Expand All @@ -54,7 +55,8 @@ 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 ^ 2) + (y ^ 2)

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

Expand All @@ -67,7 +69,8 @@ sumOfSquares x y = error "TODO!"

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

{- | Write a function that takes three numbers and returns the
difference between the biggest number and the smallest one.
Expand All @@ -81,7 +84,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 max = maximum [x,y,z]
min = minimum [x,y,z]
in max - min

{- | 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 +105,11 @@ 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 -> String -> String
subString start end str
| start < 0 = 0 end str
| end < 0 = ""
| otherwise = take (end - 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 +119,10 @@ and finds a sum of the numbers inside this string.

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

{- | 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 +137,8 @@ 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] -> String
lowerAndGreater n list =
let lower = filter (<n) list
greater = filter (>n) list
in "Lower: " ++ show lower ++ "\nGreater: " ++ show greater