Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: moocfi/haskell-mooc
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: thanhhaa/haskell-mooc
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Jul 14, 2022

  1. Solution for Set1.hs

    thanhhaa committed Jul 14, 2022
    Copy the full SHA
    17f9035 View commit details

Commits on Jul 16, 2022

  1. Summit Set2a.hs

    thanhhaa committed Jul 16, 2022
    Copy the full SHA
    9834d92 View commit details

Commits on Jul 19, 2022

  1. Done: Set2b.hs

    thanhhaa committed Jul 19, 2022
    Copy the full SHA
    4d37f56 View commit details
Showing with 95 additions and 31 deletions.
  1. +27 −12 exercises/Set1.hs
  2. +36 −11 exercises/Set2a.hs
  3. +32 −8 exercises/Set2b.hs
39 changes: 27 additions & 12 deletions exercises/Set1.hs
Original file line number Diff line number Diff line change
@@ -20,22 +20,25 @@ import Mooc.Todo
------------------------------------------------------------------------------
-- Ex 1: define variables one and two. They should have type Int and
-- values 1 and 2, respectively.
one :: Int
one = 1


two :: Int
two = 2
------------------------------------------------------------------------------
-- Ex 2: define the function double of type Integer->Integer. Double
-- should take one argument and return it multiplied by two.

double :: Integer -> Integer
double x = todo
double x = x * 2

------------------------------------------------------------------------------
-- Ex 3: define the function quadruple that uses the function double
-- from the previous exercise to return its argument multiplied by
-- four.

quadruple :: Integer -> Integer
quadruple x = todo
quadruple x = x * 4

------------------------------------------------------------------------------
-- Ex 4: define the function distance. It should take four arguments of
@@ -50,8 +53,11 @@ quadruple x = todo
-- Examples:
-- distance 0 0 1 1 ==> 1.4142135...
-- distance 1 1 4 5 ==> 5.0

distance = todo
-- distance :: Floating a => a -> a -> a -> a -> a
distance :: Double -> Double -> Double -> Double -> Double
distance x1 y1 x2 y2 = sqrt (distanceX + distanceY)
where distanceX = (x1 - x2) ^ 2
distanceY = (y1 - y2) ^ 2

------------------------------------------------------------------------------
-- Ex 5: define the function eeny that returns "eeny" for even inputs
@@ -60,15 +66,15 @@ distance = todo
-- Ps. have a look at the built in function "even"

eeny :: Integer -> String
eeny = todo
eeny x = if even x then "eeny" else "meeny"

------------------------------------------------------------------------------
-- Ex 6: here's the function checkPassword from the course material.
-- Modify it so that it accepts two passwords, "swordfish" and
-- "mellon".

checkPassword :: String -> String
checkPassword password = if password == "swordfish"
checkPassword password = if password == "swordfish" || password == "mellon"
then "You're in."
else "ACCESS DENIED!"

@@ -82,7 +88,10 @@ checkPassword password = if password == "swordfish"
-- in grams, and returns the cost in credits.

postagePrice :: Int -> Int
postagePrice = todo
postagePrice x = if x <= 500 then 250
else if x > 500 && x <= 5000
then 300 + x
else 6000

------------------------------------------------------------------------------
-- Ex 8: define a function isZero that returns True if it is given an
@@ -92,22 +101,26 @@ postagePrice = todo
--
-- Ps. remember, the type of booleans in haskell is Bool

isZero = todo
isZero :: Integer -> Bool
isZero 0 = True
isZero _ = False

------------------------------------------------------------------------------
-- Ex 9: implement using recursion a function sumTo such that
-- sumTo n
-- computes the sum 1+2+...+n

sumTo :: Integer -> Integer
sumTo = todo
sumTo 1 = 1
sumTo n = n + sumTo (n - 1)

------------------------------------------------------------------------------
-- Ex 10: power n k should compute n to the power k (i.e. n^k)
-- Use recursion.

power :: Integer -> Integer -> Integer
power = todo
power n 1 = n
power n k = n * power n (k - 1)

------------------------------------------------------------------------------
-- Ex 11: ilog3 n should be the number of times you can divide given
@@ -126,4 +139,6 @@ power = todo
-- ilog3 7 ==> 2

ilog3 :: Integer -> Integer
ilog3 = todo
ilog3 0 = 0
ilog3 1 = 1
ilog3 n = 1 + ilog3 (n `div` 3)
47 changes: 36 additions & 11 deletions exercises/Set2a.hs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ import Data.List
-- Ex 1: Define the constant years, that is a list of the values 1982,
-- 2004 and 2020 in this order.

years = todo
years = [1982, 2004, 2020]

------------------------------------------------------------------------------
-- Ex 2: define the function takeFinal, which returns the n last
@@ -32,7 +32,9 @@ years = todo
-- Hint! remember the take and drop functions.

takeFinal :: Int -> [a] -> [a]
takeFinal n xs = todo
takeFinal n xs
| n > length xs = xs
| otherwise = drop (length xs - n) xs

------------------------------------------------------------------------------
-- Ex 3: Update an element at a certain index in a list. More
@@ -46,7 +48,7 @@ takeFinal n xs = todo
-- updateAt 2 0 [4,5,6,7] ==> [4,5,0,7]

updateAt :: Int -> a -> [a] -> [a]
updateAt i x xs = todo
updateAt i x xs = take i xs ++ [x] ++ drop (i+1) xs

------------------------------------------------------------------------------
-- Ex 4: substring i j s should return the substring of s starting at
@@ -60,7 +62,7 @@ updateAt i x xs = todo
-- substring 0 4 "abcdefgh" ==> "abcd"

substring :: Int -> Int -> String -> String
substring i j s = todo
substring i j s = take (j - i) (drop i s)

------------------------------------------------------------------------------
-- Ex 5: check if a string is a palindrome. A palindrome is a string
@@ -75,7 +77,17 @@ substring i j s = todo
-- isPalindrome "AB" ==> False

isPalindrome :: String -> Bool
isPalindrome str = todo
isPalindrome str
| isEven && firstHalf == reverseEndHalfEven = True
| isEven && firstHalf /= reverseEndHalfEven = False
| isEven == False && firstHalf == reverseEndHalfOdd = True
| isEven == False && firstHalf /= reverseEndHalfOdd = False
| otherwise = True
where isEven = even (length str)
pivot = length str `div` 2
firstHalf = take pivot str
reverseEndHalfEven = reverse (drop pivot str)
reverseEndHalfOdd = reverse (drop (pivot + 1) str)

------------------------------------------------------------------------------
-- Ex 6: implement the function palindromify that chops a character
@@ -89,7 +101,9 @@ isPalindrome str = todo
-- palindromify "abracacabra" ==> "acaca"

palindromify :: String -> String
palindromify s = todo
palindromify s
| isPalindrome s = s
| otherwise = palindromify (init (tail s))

------------------------------------------------------------------------------
-- Ex 7: implement safe integer division, that is, a function that
@@ -102,7 +116,10 @@ palindromify s = todo
-- safeDiv 4 0 ==> Nothing

safeDiv :: Integer -> Integer -> Maybe Integer
safeDiv x y = todo
safeDiv x y
| y == 0 = Nothing
| otherwise = Just result
where result = x `div`y

------------------------------------------------------------------------------
-- Ex 8: implement a function greet that greets a person given a first
@@ -114,7 +131,10 @@ safeDiv x y = todo
-- greet "John" (Just "Smith") ==> "Hello, John Smith!"

greet :: String -> Maybe String -> String
greet first last = todo
greet first Nothing = "Hello, " ++ first ++ "!"
greet first (Just x) = "Hello, " ++ first ++ " " ++ x ++ "!"



------------------------------------------------------------------------------
-- Ex 9: safe list indexing. Define a function safeIndex so that
@@ -130,7 +150,9 @@ greet first last = todo
-- safeIndex ["a","b","c"] (-1) ==> Nothing

safeIndex :: [a] -> Int -> Maybe a
safeIndex xs i = todo
safeIndex xs i
| null xs || i >= length xs || i < 0 = Nothing
| otherwise = Just (last (take (i + 1) xs))

------------------------------------------------------------------------------
-- Ex 10: another variant of safe division. This time you should use
@@ -141,7 +163,8 @@ safeIndex xs i = todo
-- eitherDiv 4 0 ==> Left "4/0"

eitherDiv :: Integer -> Integer -> Either String Integer
eitherDiv x y = todo
eitherDiv x 0 = Left (show x ++ "/" ++ show 0)
eitherDiv x y = Right (div x y)

------------------------------------------------------------------------------
-- Ex 11: implement the function addEithers, which combines two values of type
@@ -158,4 +181,6 @@ eitherDiv x y = todo
-- addEithers (Left "boom") (Left "fail") ==> Left "boom"

addEithers :: Either String Int -> Either String Int -> Either String Int
addEithers a b = todo
addEithers (Right a) (Right b) = Right (a + b)
addEithers (Right a) (Left b) = Left b
addEithers (Left a) _ = Left a
40 changes: 32 additions & 8 deletions exercises/Set2b.hs
Original file line number Diff line number Diff line change
@@ -16,7 +16,9 @@ import Data.List
-- Hint! pattern matching is your friend.

binomial :: Integer -> Integer -> Integer
binomial = todo
binomial _ 0 = 1
binomial 0 k = 0
binomial n k = binomial (n-1) k + binomial (n-1) (k-1)

------------------------------------------------------------------------------
-- Ex 2: implement the odd factorial function. Odd factorial is like
@@ -27,7 +29,11 @@ binomial = todo
-- oddFactorial 6 ==> 5*3*1 ==> 15

oddFactorial :: Integer -> Integer
oddFactorial = todo
oddFactorial n
| n < 0 = -1
| n <= 2 = 1
| otherwise = oddResult * oddFactorial (oddResult - 2)
where oddResult = if even n then n - 1 else n

------------------------------------------------------------------------------
-- Ex 3: implement the Euclidean Algorithm for finding the greatest
@@ -59,7 +65,11 @@ oddFactorial = todo
-- * https://en.wikipedia.org/wiki/Euclidean_algorithm

myGcd :: Integer -> Integer -> Integer
myGcd = todo
myGcd 0 b = b
myGcd a 0 = a
myGcd a b = myGcd larger smaller
where larger = if a > b then a - b else a
smaller = if a > b then b else b - a

------------------------------------------------------------------------------
-- Ex 4: Implement the function leftpad which adds space characters
@@ -75,7 +85,9 @@ myGcd = todo
-- * you can compute the length of a string with the length function

leftpad :: String -> Int -> String
leftpad = todo
leftpad s n
| n <= length s = s
| otherwise = " " ++ leftpad s (n - 1)

------------------------------------------------------------------------------
-- Ex 5: let's make a countdown for a rocket! Given a number, you
@@ -91,7 +103,11 @@ leftpad = todo
-- * you'll probably need a recursive helper function

countdown :: Integer -> String
countdown = todo
countdown n = countdown' n ""

countdown' :: Integer -> String -> String
countdown' 0 result = "Ready! " ++ result ++ "Liftoff!"
countdown' n result = countdown' (n - 1) (result ++ show n ++ "... ")

------------------------------------------------------------------------------
-- Ex 6: implement the function smallestDivisor that returns the
@@ -109,16 +125,23 @@ countdown = todo
-- Hint: remember the mod function!

smallestDivisor :: Integer -> Integer
smallestDivisor = todo
smallestDivisor n = smallestDivisor' n n n

smallestDivisor' :: Integer -> Integer -> Integer -> Integer
smallestDivisor' _ 1 result = result
smallestDivisor' n m result = case n `mod` m of 0 -> smallestDivisor' n (m-1) m
_ -> smallestDivisor' n (m-1) result

------------------------------------------------------------------------------
-- Ex 7: implement a function isPrime that checks if the given number
-- is a prime number. Use the function smallestDivisor.
--
-- Ps. 0 and 1 are not prime numbers

isPrime :: Integer -> Bool
isPrime = todo
isPrime 0 = False
isPrime 1 = False
isPrime n = smallestDivisor n == n

------------------------------------------------------------------------------
-- Ex 8: implement a function biggestPrimeAtMost that returns the
@@ -133,4 +156,5 @@ isPrime = todo
-- biggestPrimeAtMost 10 ==> 7

biggestPrimeAtMost :: Integer -> Integer
biggestPrimeAtMost = todo
biggestPrimeAtMost n = if isPrime n then n
else biggestPrimeAtMost (n - 1)