Skip to content

Instantly share code, notes, and snippets.

@kvnyijia
Forked from banacorn/Chapter-1-6.hs
Last active July 24, 2020 02:59
Show Gist options
  • Save kvnyijia/f6e0b5d1992b3191a58cd73266902349 to your computer and use it in GitHub Desktop.
Save kvnyijia/f6e0b5d1992b3191a58cd73266902349 to your computer and use it in GitHub Desktop.
-- This exercise covers the first 6 chapters of "Learn You a Haskell for Great Good!"
-- Chapter 1 - http://learnyouahaskell.com/introduction
-- Chapter 2 - http://learnyouahaskell.com/starting-out
-- Chapter 3 - http://learnyouahaskell.com/types-and-typeclasses
-- Chapter 4 - http://learnyouahaskell.com/syntax-in-functions
-- Chapter 5 - http://learnyouahaskell.com/recursion
-- Chapter 6 - http://learnyouahaskell.com/higher-order-functions
-- Download this file and then type ":l Chapter-1-6.hs" in GHCi to load this exercise
-- Some of the definitions are left "undefined", you should replace them with your answers.
-- Find the penultimate (second-to-last) element in list xs
penultimate xs = last (init xs)
-- Find the antepenultimate (third-to-last) element in list xs
antepenultimate xs = last . init . init $ xs
-- Left shift list xs by 1
-- For example, "shiftLeft [1, 2, 3]" should return "[2, 3, 1]"
shiftLeft xs = tail . take (length xs + 1) . cycle $ xs
-- Left shift list xs by n
-- For example, "rotateLeft 2 [1, 2, 3]" should return "[3, 1, 2]"
rotateLeft n xs = (drop n) . take (length xs + n) . cycle $ xs
-- Insert element x in list xs at index k
-- For example, "insertElem 100 3 [0,0,0,0,0]" should return [0,0,0,100,0,0]
insertElem x k xs = (take k xs) ++ [x] ++ (drop k xs)
-- Here we have a type for the 7 days of the week
-- Try typeclass functions like "show" or "maxBound" on them
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
deriving (Eq, Ord, Show, Bounded, Enum)
-- Note that if you try "succ Sun", you should get an error, because "succ" is not defined on "Sun"
-- Define "next", which is like "succ", but returns "Mon" on "next Sun"
next :: Day -> Day
next day | day == Mon = Tue
| day == Tue = Wed
| day == Wed = Thu
| day == Thu = Fri
| day == Fri = Sat
| day == Sat = Sun
| day == Sun = Mon
-- Return "True" on weekend
isWeekend :: Day -> Bool
isWeekend day | day == Sat || day == Sun = True
| otherwise = False
data Task = Work | Shop | Play deriving (Eq, Show)
-- You are given a schedule, which is a list of pairs of Tasks and Days
schedule :: [(Task, Day)]
schedule = [(Shop, Fri), (Work, Tue), (Play, Mon), (Play, Fri)]
-- However, the schedule is a mess
-- Sort the schedule by Day, and return only a list of Tasks.
-- If there are many Tasks in a Day, you should keep its original ordering
-- For example, "sortTask schedule" should return "[(Play, Mon), (Work, Tue), (Shop, Fri), (Play, Fri)]"
sortTask :: [(Task, Day)] -> [(Task, Day)]
sortTask [] = []
sortTask [x] = [x]
sortTask xs = merge (sortTask ys) (sortTask zs)
where (ys, zs) = (take half xs, drop half xs)
where half = length xs `div` 2
merge :: [(Task, Day)] -> [(Task, Day)] -> [(Task, Day)]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys) = if (snd x) <= (snd y) then x : merge xs (y:ys)
else y : merge (x:xs) ys
-- This function converts days to names, like "show", but a bit fancier
-- For example, "nameOfDay Mon" should return "Monday"
nameOfDay :: Day -> String
nameOfDay x | x == Mon = "Monday"
| x == Tue = "Tuesday"
| x == Wed = "Wednesday"
| x == Thu = "Thursday"
| x == Fri = "Friday"
| x == Sat = "Saturday"
| x == Sun = "Sunday"
-- You shouldn't be working on the weekends
-- Return "False" if the Task is "Work" and the Day is "Sat" or "Sun"
labourCheck :: Task -> Day -> Bool
labourCheck task day | task == Work && (day == Sat || day == Sun) = False -- () required
| otherwise = True
-- Raise x to the power y using recursion
-- For example, "power 3 4" should return "81"
power :: Int -> Int -> Int
power 0 0 = error "NaN"
power 0 _ = 0
power x 0 = 1
power x y = x * power x (y-1)
-- Convert a list of booleans (big-endian) to a interger using recursion
-- For example, "convertBinaryDigit [True, False, False]" should return 4
convertBinaryDigit :: [Bool] -> Int
convertBinaryDigit [True] = 1
convertBinaryDigit [False] = 0
convertBinaryDigit (x:xs) = value + convertBinaryDigit xs
where value = if x == True then power 2 (length xs) else 0
-- Create a fibbonaci sequence of length N in reverse order
-- For example, "fib 5" should return "[3, 2, 1, 1, 0]"
fib :: Int -> [Int]
fib 1 = [0]
fib 2 = [1,0]
fib n = head p1 + head p2 : p1
where p1 = fib (n-1)
p2 = fib (n-2)
-- Determine whether a given list is a palindrome
-- For example, "palindrome []" or "palindrome [1, 3, 1]" should return "True"
palindrome :: Eq a => [a] -> Bool
palindrome xs = xs == re
where re = reverse xs
-- Map the first component of a pair with the given function
-- For example, "mapFirst (+3) (4, True)" should return "(7, True)"
mapFirst :: (a -> b) -> (a, c) -> (b, c)
mapFirst f pair = (f . fst $ pair, snd pair)
-- Devise a function that has the following type
someFunction :: (a -> b -> c) -> (a -> b) -> a -> c
someFunction f g x = f x (g x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment