diff --git a/src/Lecture1.hs b/src/Lecture1.hs index 0d023695..14c9c7ca 100644 --- a/src/Lecture1.hs +++ b/src/Lecture1.hs @@ -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 @@ -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. @@ -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. @@ -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 @@ -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. @@ -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 @@ -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" + diff --git a/src/Lecture2.hs b/src/Lecture2.hs index 4995d0a3..43173783 100644 --- a/src/Lecture2.hs +++ b/src/Lecture2.hs @@ -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. @@ -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' @@ -293,3 +289,4 @@ Folding" optimization on the given expression. -} constantFolding :: Expr -> Expr constantFolding = error "TODO" + diff --git a/src/Lecture3.hs b/src/Lecture3.hs index ed393245..b60a7e3d 100644 --- a/src/Lecture3.hs +++ b/src/Lecture3.hs @@ -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 @@ -250,3 +247,4 @@ Just [8,9,10] -} apply = error "TODO" +