diff --git a/src/Lecture1.hs b/src/Lecture1.hs index b27514ed..dc8f1195 100644 --- a/src/Lecture1.hs +++ b/src/Lecture1.hs @@ -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 @@ -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. @@ -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. @@ -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 @@ -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. @@ -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 @@ -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 + in "Lower: " ++ show lower ++ "\nGreater: " ++ show greater