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

solve Lecture1 #1

Open
wants to merge 2 commits 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
39 changes: 33 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 :: Int -> [Char] -> [Char]
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 :: Num a => a -> a -> a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Num is explained in the following lectures, so you are one step ahead 😸
In here Int -> Int -> Int or Integer -> Integer -> Integer would also work, as a more specific type 🙂

sumOfSquares x y = x * x + y * y

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

Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely correct 👍🏻
Alternatively, you can use the infix form of a function by putting it inside backticks 🙂

Suggested change
mod (abs n) 10
abs 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 +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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numeric literals in Haskell are polymorphic. That's why you have lots of GHC warnings about defaulting constraints.

To fix this, you can either specify the type of iter explicitly or specialize literals here

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for great instuctions.
Actually I use HLS to specify types and formmating.

Here refctoring iter

    iter [] lower greater =
      show n
        ++ " is greater than "
        ++ show lower
        ++ " elements and lower than "
        ++ show greater
        ++ " elements"
    iter (h : t) lower greater
      | h < n = iter  t (lower + 1) greater
      | h > n = iter  t lower (greater + 1)
      | otherwise = iter  t lower greater

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! And yes, HLS can be really helpful 👏🏻

where
iter n [] lower greater = show n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the number n that you pass in recursive calls doesn't change. You always pass the same number. In that case, you can remove this extra argument and use n from the function instead. It's possible because functions in where can see all the arguments of the top-level function 🔍

++ " 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