From 9bf71c48bcc01aedb2dddbd952eee6b4139e21e3 Mon Sep 17 00:00:00 2001 From: IvoTod Date: Tue, 1 Nov 2016 23:40:40 +0200 Subject: [PATCH 1/5] Upload homework --- exercises/secret-lang/Secret-lang.hs | 40 +++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/exercises/secret-lang/Secret-lang.hs b/exercises/secret-lang/Secret-lang.hs index 61c1406..3ee5f9b 100644 --- a/exercises/secret-lang/Secret-lang.hs +++ b/exercises/secret-lang/Secret-lang.hs @@ -1,32 +1,58 @@ import Data.Char(toLower) +vowels = "aeiou" 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 == True = True +isVowel _ = False isConsonant :: Char -> Bool -isConsonant = undefined +isConsonant char + | isVowel char == False && isChar char == True = True + | otherwise = False encode :: String -> String -encode = undefined +encode [] = [] +encode (first:rest) + | isConsonant first == True = first : 'o' : first : (encode rest) + | otherwise = first:(encode rest) -- Bonus #1 encode' :: String -> String -encode' = undefined +encode' [] = [] +encode' (first:rest) + | isConsonant first == True = 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 -decode = undefined +decode [] = [] +decode (x : 'o' : y : rest) + | x == y = x : decode (dropN 2 ('o':y:rest)) + | otherwise = x : 'o' : y : (decode (rest)) +decode (first : rest) = first : decode (rest) From 0c29599256af346220c2cecf95ddf7c6dc56c8b5 Mon Sep 17 00:00:00 2001 From: IvoTod Date: Wed, 2 Nov 2016 01:28:03 +0200 Subject: [PATCH 2/5] Refactor code --- exercises/secret-lang/Secret-lang.hs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/exercises/secret-lang/Secret-lang.hs b/exercises/secret-lang/Secret-lang.hs index 3ee5f9b..e4ec7ba 100644 --- a/exercises/secret-lang/Secret-lang.hs +++ b/exercises/secret-lang/Secret-lang.hs @@ -15,21 +15,19 @@ isInString char (first:rest) isVowel :: Char -> Bool -isVowel char - | isInString (toLower char) vowels == True = True -isVowel _ = False +isVowel char = isInString (toLower char) vowels isConsonant :: Char -> Bool isConsonant char - | isVowel char == False && isChar char == True = True + | not (isVowel char) && isChar char = True | otherwise = False encode :: String -> String encode [] = [] encode (first:rest) - | isConsonant first == True = first : 'o' : first : (encode rest) + | isConsonant first = first : 'o' : first : (encode rest) | otherwise = first:(encode rest) @@ -37,7 +35,7 @@ encode (first:rest) encode' :: String -> String encode' [] = [] encode' (first:rest) - | isConsonant first == True = first : 'o' : toLower first : (encode' rest) + | isConsonant first = first : 'o' : toLower first : (encode' rest) | otherwise = first:(encode' rest) From da63f1d1eceecd8758a9a4c71c1296172af285ea Mon Sep 17 00:00:00 2001 From: IvoTod Date: Wed, 2 Nov 2016 01:55:41 +0200 Subject: [PATCH 3/5] Fix decode decoding vowel sequences --- exercises/secret-lang/Secret-lang.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/secret-lang/Secret-lang.hs b/exercises/secret-lang/Secret-lang.hs index e4ec7ba..9c34fb9 100644 --- a/exercises/secret-lang/Secret-lang.hs +++ b/exercises/secret-lang/Secret-lang.hs @@ -51,6 +51,6 @@ dropN number (first:rest) = dropN (number-1) rest decode :: String -> String decode [] = [] decode (x : 'o' : y : rest) - | x == y = x : decode (dropN 2 ('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) From 5fbf67cab39f3a309873af70f42ac4d3382399cc Mon Sep 17 00:00:00 2001 From: IvoTod Date: Wed, 30 Nov 2016 01:43:44 +0200 Subject: [PATCH 4/5] Add initial MdTable --- exercises/md-table/MdTable.hs | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 exercises/md-table/MdTable.hs diff --git a/exercises/md-table/MdTable.hs b/exercises/md-table/MdTable.hs new file mode 100644 index 0000000..de0e0a8 --- /dev/null +++ b/exercises/md-table/MdTable.hs @@ -0,0 +1,51 @@ +{-# 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!"] + ] From f4648c9ac4637654c87ef47a9fc0fa959c5f76a7 Mon Sep 17 00:00:00 2001 From: IvoTod Date: Wed, 30 Nov 2016 02:05:47 +0200 Subject: [PATCH 5/5] Fix permutations --- exercises/md-table/MdTable.hs | 2 -- exercises/permutations/perm.hs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/exercises/md-table/MdTable.hs b/exercises/md-table/MdTable.hs index de0e0a8..cf2c893 100644 --- a/exercises/md-table/MdTable.hs +++ b/exercises/md-table/MdTable.hs @@ -24,13 +24,11 @@ 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 diff --git a/exercises/permutations/perm.hs b/exercises/permutations/perm.hs index 91c701e..03a5444 100644 --- a/exercises/permutations/perm.hs +++ b/exercises/permutations/perm.hs @@ -4,4 +4,4 @@ import Data.List hiding (permutations) permutations :: [Int] -> [[Int]] permutations [] = [[]] -permutations (x:xs) = [ y:ys | y <- (x:xs), ys <- permutations xs] +permutations xs = [ y:ys | y <- xs, ys <- permutations (delete y xs)]