Created
May 13, 2024 03:46
-
-
Save genetsai95/8cecea25a4c4d5c7b9b9275b4ab6f203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This exercise covers the first 6 and the 8th 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 | |
-- Chapter 8 - http://learnyouahaskell.com/making-our-own-types-and-typeclasses | |
-- Download this file and then type ":l FLOLAC-24-Tasks.hs" in GHCi to load this exercise | |
-- Some of the definitions are left "undefined", you should replace them with your answers. | |
-- 0. Example: find the penultimate (second-to-last) element in list xs | |
penultimate xs = last (init xs) | |
-- 1. Find the antepenultimate (third-to-last) element in list xs | |
antepenultimate xs = undefined | |
-- 2. Left shift list xs by n | |
-- For example, "rotateLeft 2 [1, 2, 3]" should return "[3, 1, 2]" | |
rotateLeft n xs = undefined | |
-- 3. 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 = undefined | |
-- 4. 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 = undefined | |
-- 5. Raise x to the power y using recursion | |
-- For example, "power 3 4" should return "81" | |
power :: Int -> Int -> Int | |
power x y = undefined | |
-- 6. 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 bits = undefined | |
-- 7. 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 = undefined | |
-- 8. 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 = undefined | |
-- 9. Devise a function that has the following type | |
someFunction :: (a -> (b -> c) -> d) -> (b -> a -> c) -> a -> d | |
someFunction = undefined | |
-- Here we have a type for 10 Heavenly Stems ("天干"), 12 Earthly Branches ("地支"), and a pair type of them. | |
-- Try typeclass functions like "show" or "maxBound" on them | |
data Stems = Chia | Yi | Bing | Ding | Wu | Ji | Geng | Xin | Ren | Gui | |
deriving (Eq, Ord, Show, Bounded, Enum) | |
data Branches = Zi | Chou | Yin | Mao | Chen | Si | Wu' | Wei | Shen | You | Shu | Hai | |
deriving (Eq, Ord, Show, Bounded, Enum) | |
type StemsAndBranches = (Stems , Branches) | |
-- 10. Note that if you try "succ Gui" or "succ Hai", you should get an error, because "succ" is not defined on either "Gui" or "Hai" | |
-- Define "next" for "StemsAndBranches", which shift both the stem and the branch to the next one, and cycle to the first one if it meets the end. | |
-- For example, "next (Bing, Yin)" should be "(Ding, Mao)", and "next (Ding, Hai)" should be "(Wu, Zi)". | |
next :: StemsAndBranches -> StemsAndBranches | |
next (stem, branch) = undefined | |
-- 11. If you keep iterating with "next", you will be in a sexagenary cycle. | |
-- Only half of the pairs will be traversed (60 pairs in total.) | |
-- Determine whether a pair is in the cycle that starts from "(Chia, Zi)". Return "True" if the pair is in the cycle, otherwise return "False". | |
inCycle :: StemsAndBranches -> Bool | |
inCycle (stem, branch) = undefined | |
-- Here is an algebraic datatype representing trees. | |
-- Be careful, these trees are somehow different from those defined in the book! | |
-- Apparently our trees are better, they have leaves, and data is stored on leaves. | |
data Tree a = Leaf a | Node (Tree a) (Tree a) | |
-- 12. What is the map function as well as the Functor instance for this tree? | |
-- (You may want to lookup the Functor typeclass.) | |
instance Functor Tree where | |
fmap = undefined | |
-- We say a tree is flattenable if it can be turned into a list | |
-- which contains all elements originally in the tree. | |
data List a = Nil | Cons a (List a) | |
-- We can define a typeclass to express that. | |
class Flattenable t where | |
flatten :: t a -> List a | |
-- 13. Show that our Tree is flattenable: | |
instance Flattenable Tree where | |
flatten = undefined | |
-- 14. Define a type of trees that have leaves and two kinds of nodes: | |
-- one with two branches and another with three branches. | |
-- Your tree should have three constructors. | |
-- You can choose where to store data (either on leaves, nodes, or both). | |
-- | |
-- data TwoThreeTree = ... | |
-- 15. Show that the datatype you just defined is flattenable: | |
-- | |
-- instance Flattenable TwoThreeTree where | |
-- flatten = ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment