diff --git a/exercises/md-table/MdTable.hs b/exercises/md-table/MdTable.hs new file mode 100644 index 0000000..cf2c893 --- /dev/null +++ b/exercises/md-table/MdTable.hs @@ -0,0 +1,49 @@ +{-# OPTIONS_GHC -Wall #-} + +module MdTable where + +import Data.List (intercalate) + +getMaxColumns :: [[String]] -> Int +getMaxColumns [] = 0 +getMaxColumns xs = maximum [length x | x <- xs] + +getMaxColumnLength :: [[String]] -> Int +getMaxColumnLength [] = 0 +getMaxColumnLength xs = maximum [length x | x <- concat xs] + +addEmptyStrings :: [String] -> Int -> [String] +addEmptyStrings xs 0 = xs +addEmptyStrings [] n = "" : (addEmptyStrings [] (n-1)) +addEmptyStrings (x:xs) n = x : (addEmptyStrings xs (n)) + +equalizeColumnCount :: [[String]] -> [[String]] +equalizeColumnCount xs = [addEmptyStrings x (getMaxColumns xs - length x) | x <- xs] + +equalizeColumnSize :: [[String]] -> [[String]] +equalizeColumnSize [] = [] +equalizeColumnSize (x:xs) = [pad y | y <- x] : (equalizeColumnSize xs) + where pad z = padWith ' ' (getMaxColumnLength (x:xs)) z + +toLine :: [String] -> String +toLine [] = "|" +toLine (x:xs) = "| " ++ x ++ " " ++ toLine xs + +padWith :: (Eq a) => a -> Int -> [a] -> [a] +padWith _ 0 x | x == [] = [] + | otherwise = x +padWith pad n [] = pad : padWith pad (n-1) [] +padWith pad n (x:xs) = x : padWith pad (n-1) xs + + +layoutTable :: [[String]] -> String +layoutTable xs = intercalate "\n" [toLine x | x <- ys] + where ys = equalizeColumnSize (equalizeColumnCount xs) + +table2 :: [[String]] +table2 = [ + [ "Item", "Price" ], + [ "iPhone", "$1", "Not in stock" ], + [ "iPad", "$599" ], + [ "CAUTION: It's a scam!"] + ] diff --git a/exercises/permutations/perm.hs b/exercises/permutations/perm.hs index 12547ba..03a5444 100644 --- a/exercises/permutations/perm.hs +++ b/exercises/permutations/perm.hs @@ -3,5 +3,5 @@ module Permutations where import Data.List hiding (permutations) permutations :: [Int] -> [[Int]] -permutations = undefined - +permutations [] = [[]] +permutations xs = [ y:ys | y <- xs, ys <- permutations (delete y xs)] diff --git a/exercises/secret-lang/Secret-lang.hs b/exercises/secret-lang/Secret-lang.hs index 987c26d..30a35f2 100644 --- a/exercises/secret-lang/Secret-lang.hs +++ b/exercises/secret-lang/Secret-lang.hs @@ -1,32 +1,64 @@ import Data.Char(toLower) +<<<<<<< HEAD +vowels = "aeiou" + +======= +>>>>>>> c48c6b3660bd9b1429acba58cb5198a8ec08411d isChar :: Char -> Bool -isChar = undefined +isChar char + | toLower char >= 'a' && toLower char <= 'z' = True +isChar _ = False + +isInString :: Char -> String -> Bool +isInString char [] = False +isInString char (first:rest) + | char == first = True + | char /= first = isInString char rest isVowel :: Char -> Bool -isVowel = undefined +isVowel char = isInString (toLower char) vowels isConsonant :: Char -> Bool -isConsonant = undefined +isConsonant char + | not (isVowel char) && isChar char = True + | otherwise = False encode :: String -> String -encode = undefined +encode [] = [] +encode (first:rest) + | isConsonant first = first : 'o' : first : (encode rest) + | otherwise = first:(encode rest) -- Bonus #1 encode' :: String -> String -encode' = undefined +encode' [] = [] +encode' (first:rest) + | isConsonant first = first : 'o' : toLower first : (encode' rest) + | otherwise = first:(encode' rest) -- Bonus #2 dropN :: Int -> String -> String -dropN = undefined +dropN number [] = [] +dropN 0 string = string +dropN number (first:rest) = dropN (number-1) rest + -- Assume we'll be decoding only valid words decode :: String -> String +<<<<<<< HEAD +decode [] = [] +decode (x : 'o' : y : rest) + | x == y && isConsonant x = x : decode (dropN 2 ('o':y:rest)) + | otherwise = x : 'o' : y : (decode (rest)) +decode (first : rest) = first : decode (rest) +======= decode = undefined +>>>>>>> c48c6b3660bd9b1429acba58cb5198a8ec08411d