From 0aaa1859ee00b7864289476f9fd5374fb296da7c Mon Sep 17 00:00:00 2001 From: racagogi Date: Thu, 23 Feb 2023 17:28:00 +0900 Subject: [PATCH 1/2] solve Lecture1 --- src/Lecture1.hs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Lecture1.hs b/src/Lecture1.hs index b27514ed..dc702e08 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 :: Int -> [Char] -> [Char] 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 :: Num a => a -> a -> a +sumOfSquares x y = x*x+y*y {- | Implement a function that returns the last digit of a given number. @@ -67,7 +69,9 @@ sumOfSquares x y = error "TODO!" -} -- DON'T FORGET TO SPECIFY THE TYPE IN HERE -lastDigit n = error "lastDigit: Not implemented!" +lastDigit :: Integral a => a -> a +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 +85,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 :: (Num a, Ord a) => a -> a -> a -> a +minmax x y z = + let biggest = maximum [x,y,z] + smallest = minimum [x,y,z] + in biggest - smallest {- | 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 +106,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 -> [Char] -> [Char] +subString start end str + | end < 0 || start > end = "" + | start <= 0 = take (end + 1) str + | otherwise = drop start (take (end+1) str) {- | Write a function that takes a String — space separated numbers, and finds a sum of the numbers inside this string. @@ -108,7 +120,9 @@ and finds a sum of the numbers inside this string. The string contains only spaces and/or numbers. -} -strSum str = error "TODO" +strSum :: (Num a, Read a) => String -> a +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 +137,17 @@ 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 :: (Show a, Ord a) => a -> [a] -> [Char] +lowerAndGreater n list = + iter n list 0 0 + where + iter n [] lower greater = show n + ++ " is greater than " + ++ show lower + ++ " elements and lower than " + ++ show greater + ++ " elements" + iter n (h:t) lower greater + | h < n = iter n t (lower + 1) greater + | h > n = iter n t lower (greater + 1) + | otherwise = iter n t lower greater From 9be1bf21bcc9d5a572c646d3d1c18cac016fb961 Mon Sep 17 00:00:00 2001 From: racagogi Date: Fri, 24 Feb 2023 00:43:22 +0900 Subject: [PATCH 2/2] Update src/Lecture1.hs Co-authored-by: Dmitrii Kovanikov --- src/Lecture1.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Lecture1.hs b/src/Lecture1.hs index dc702e08..36afc14b 100644 --- a/src/Lecture1.hs +++ b/src/Lecture1.hs @@ -56,7 +56,7 @@ is 25. -} -- DON'T FORGET TO SPECIFY THE TYPE IN HERE sumOfSquares :: Num a => a -> a -> a -sumOfSquares x y = x*x+y*y +sumOfSquares x y = x * x + y * y {- | Implement a function that returns the last digit of a given number.