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

Upload Homework for MDFormat #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 23 additions & 14 deletions exercises/lists/Lists.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,33 @@ null [] = True
null _ = False

length :: [Int] -> Int
length = undefined
length [] = 0
length [_] = 1
length (_:xs) = 1 + length(xs)


take :: Int -> [Int] -> [Int]
take _ [] = []
take 1 (x:xs) = [x]
take 1 (x:_) = [x]
take n _ | n < 1 = []
take n (x:xs) = x : take (n-1) xs

take' :: Int -> [Int] -> [Int]
take' 0 _ = []
take' n [] = []
take' _ [] = []
take' n (x:xs) | n < 0 = []
| otherwise = x : take' (n-1) xs


drop :: Int -> [Int] -> [Int]
drop = undefined

drop _ [] = []
drop 1 (_:xs) = xs
drop n (_:xs) = drop (n - 1) xs

elem :: Int -> [Int] -> Bool
elem = undefined

elem _ [] = False
elem n (x:xs) | n == x = True
| otherwise = elem n xs

reverseHelper :: [Int] -> [Int] -> [Int]
reverseHelper acc [] = acc
Expand All @@ -80,28 +84,33 @@ concat = undefined


replicate :: Int -> Int -> [Int]
replicate = undefined

replicate 0 _ = []
replicate 1 n = [n]
replicate k n = n : replicate (k - 1) n

interleave :: [Int] -> [Int] -> [Int]
interleave = undefined


sum :: [Int] -> Int
sum = undefined
sum [] = 0
sum [x] = x
sum (x:xs) = x + sum xs


maximum :: [Int] -> Int
maximum = undefined

maximum [] = 0
maximum (x:xs) | x > maximum xs = x
| otherwise = maximum xs

nub :: [Int] -> [Int]
nub = undefined


delete :: Int -> [Int] -> [Int]
delete = undefined

delete _ [] = []
delete 1 (_:xs) = xs
delete n (x:xs) = x : delete (n - 1) xs

difference :: [Int] -> [Int] -> [Int]
difference = undefined
Expand Down
77 changes: 75 additions & 2 deletions exercises/md-processor/mdparser.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module MDProcessor where
import Data.Char(toLower)

data Lang = Haskell | C | Python | None deriving (Eq, Show)
data HeadingSize = H1 | H2 | H3 | H4 deriving (Eq, Show)
Expand All @@ -15,16 +16,31 @@ data Element =
| HorizontalRule
deriving (Eq, Show)

myText :: Element
myText = Italic (Bold (Underline (Text "This is a test")))
-- myList :: Element
-- myList = Ordered

myText :: Element
myText = Bold(Text "This is a test")
-- myText = List Unordered [Text "Elemen1", Text "Element2", Text "Element 3"]

headingPrefix :: HeadingSize -> String
headingPrefix H1 = "#"
headingPrefix H2 = "##"
headingPrefix H3 = "###"
headingPrefix H4 = "####"

htmlHeadingPrefix :: HeadingSize -> String
htmlHeadingPrefix H1 = "<h1>"
htmlHeadingPrefix H2 = "<h2>"
htmlHeadingPrefix H3 = "<h3>"
htmlHeadingPrefix H4 = "<h4>"

htmlHeadingPostfix :: HeadingSize -> String
htmlHeadingPostfix H1 = "</h1>"
htmlHeadingPostfix H2 = "</h2>"
htmlHeadingPostfix H3 = "</h3>"
htmlHeadingPostfix H4 = "</h4>"

lang :: Lang -> String
lang Haskell = "hs"
lang C = "c"
Expand All @@ -44,3 +60,60 @@ render2md (List mode items) = renderList mode items
--renderList :: ListMode -> [Element] -> String
renderList Ordered l = unlines [show i ++ ". " ++ render2md (l !! (i-1)) | i <- [1..length l]]
renderList Unordered l = unlines ["- " ++ render2md x | x <- l]

render2html :: Element -> String
render2html (Text s) = "<p>" ++ s ++ "</p>"
render2html (Bold el) = "<b>" ++ render2html el ++ "</b>"
render2html (Italic el) = "<i>" ++ render2html el ++ "</i>"
render2html (Underline el) = "<u>" ++ render2html el ++ "</u>"
-- render2html (Heading size str) = htmlHeadingPrefix ++ render2html str ++ htmlHeadingPostfix
render2html (CodeBlock _ code) = "<code>" ++ unlines code ++ "</code>"
render2html (List mode items) | mode == Ordered = "<ol>" ++ renderList Ordered items ++ "</ol>"
| mode == Unordered = "<ul>" ++ renderList Unordered items ++ "</ul>"
where
renderList Ordered l = unlines ["<li>" ++ render2html (l !! (i - 1)) ++"</li>" | i <- [1..length l]]
renderList Unordered l = unlines ["<li>" ++ render2html (l !! (i - 1)) ++"</li>" | i <- [1..length l]]

robberify :: Element -> Element
robberify (Text s) = Text (encode' s)
robberify (Bold el) = Bold (robberify el)
robberify (Italic el) = Italic (robberify el)
robberify (Underline el) = Underline (robberify el)
-- robberify (Heading size el) = Heading size (robberify el) I will work on those later :)
-- robberify (CodeBlock pl el) = CodeBlock pl (encode' el)
-- robberify (List el) = List (robberify el)
-- robberify (x el) = x robberify el -- Ask why this doesn't work!?!

-- toc :: [Element] -> [Element]
-- -- toc [] = List Ordered []
-- toc ((Heading H4 x) : xs) = Text (x) : toc(xs)
-- toc (x : xs) = toc(xs)

isChar :: Char -> Bool
isChar ch
|toLower ch >= 'a' && toLower ch <= 'z' = True
isChar _ = False

isVowel :: Char -> Bool
isVowel ch
| toLower ch == 'a' = True
| toLower ch == 'e' = True
| toLower ch == 'u' = True
| toLower ch == 'i' = True
| toLower ch == 'o' = True
| toLower ch == 'y' = True
isVowel _ = False


isConsonant :: Char -> Bool
isConsonant ch
| not (isChar ch) = False
| isVowel ch = False
isConsonant _ = True

-- Bonus #1
encode' :: String -> String
encode' [] = []
encode' (x:xs)
| isConsonant x = x : 'o' : toLower x : (encode' xs)
encode' (x:xs) = x : (encode' xs)
42 changes: 33 additions & 9 deletions exercises/secret-lang/Secret-lang.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
import Data.Char(toLower)

isChar :: Char -> Bool
isChar = undefined

isChar ch
|toLower ch >= 'a' && toLower ch <= 'z' = True
isChar _ = False

isVowel :: Char -> Bool
isVowel = undefined
isVowel ch
| toLower ch == 'a' = True
| toLower ch == 'e' = True
| toLower ch == 'u' = True
| toLower ch == 'i' = True
| toLower ch == 'o' = True
| toLower ch == 'y' = True
isVowel _ = False


isConsonant :: Char -> Bool
isConsonant = undefined
isConsonant ch
| not (isChar ch) = False
| isVowel ch = False
isConsonant _ = True


encode :: String -> String
encode = undefined

encode [] = []
encode (x:xs)
| isConsonant x = x : 'o' : x : (encode xs)
encode (x:xs) = x : (encode xs)

-- Bonus #1
encode' :: String -> String
encode' = undefined

encode' [] = []
encode' (x:xs)
| isConsonant x = x : 'o' : toLower x : (encode' xs)
encode' (x:xs) = x : (encode' xs)

-- Bonus #2
dropN :: Int -> String -> String
dropN = undefined
dropN _ [] = []
dropN 0 anyString = anyString
dropN a (x:xs) = dropN (a - 1) xs


-- Assume we'll be decoding only valid words
decode :: String -> String
<<<<<<< HEAD
decode [] = []
decode (x:'o':y:xs)
| x == y = x : decode xs
decode (x:xs) = x:decode xs
=======
decode = undefined

>>>>>>> c48c6b3660bd9b1429acba58cb5198a8ec08411d