-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely correct 👍🏻
Suggested change
|
||||||
|
||||||
{- | 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for great instuctions. Here refctoring 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that the number |
||||||
++ " 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 |
There was a problem hiding this comment.
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
orInteger -> Integer -> Integer
would also work, as a more specific type 🙂