From a9ee47b61558efe09aee23f5671aabd0c3747e8a Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Tue, 17 Mar 2015 11:05:29 +0800 Subject: p -> h --- h1.hs | 9 +++++++++ h10.hs | 20 ++++++++++++++++++++ h11.hs | 15 +++++++++++++++ h2.hs | 11 +++++++++++ h3.hs | 7 +++++++ h4.hs | 3 +++ h5.hs | 3 +++ h6.hs | 11 +++++++++++ h7.hs | 5 +++++ h8.hs | 12 ++++++++++++ h9.hs | 13 +++++++++++++ p1.hs | 9 --------- p10.hs | 20 -------------------- p11.hs | 15 --------------- p2.hs | 11 ----------- p3.hs | 7 ------- p4.hs | 3 --- p5.hs | 3 --- p6.hs | 11 ----------- p7.hs | 5 ----- p8.hs | 12 ------------ p9.hs | 13 ------------- 22 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 h1.hs create mode 100644 h10.hs create mode 100644 h11.hs create mode 100644 h2.hs create mode 100644 h3.hs create mode 100644 h4.hs create mode 100644 h5.hs create mode 100644 h6.hs create mode 100644 h7.hs create mode 100644 h8.hs create mode 100644 h9.hs delete mode 100644 p1.hs delete mode 100644 p10.hs delete mode 100644 p11.hs delete mode 100644 p2.hs delete mode 100644 p3.hs delete mode 100644 p4.hs delete mode 100644 p5.hs delete mode 100644 p6.hs delete mode 100644 p7.hs delete mode 100644 p8.hs delete mode 100644 p9.hs diff --git a/h1.hs b/h1.hs new file mode 100644 index 0000000..f0216c2 --- /dev/null +++ b/h1.hs @@ -0,0 +1,9 @@ + +myLast :: [a] -> a +myLast [] = error "Empty List" +myLast [x] = x +myLast (x:xs) = myLast xs + +myLast' = foldl1 (flip const) + +myLast'' = foldl1 (curry snd) diff --git a/h10.hs b/h10.hs new file mode 100644 index 0000000..68ffade --- /dev/null +++ b/h10.hs @@ -0,0 +1,20 @@ +import Control.Applicative +import Control.Arrow +import Data.List + +encode :: Eq a => [a] -> [(a,Int)] +encode = foldr elim [] + where + elim e [] = [(e,1)] + elim e p@(n:ns) + | e == fst n = (e,1 + (snd n)):ns + | otherwise = (e,1):p + +encode' (x:xs) = let (first,rest) = span (==x) xs + in (x,1+(length first)) : encode' rest + +encode' [] = [] + +encode'' xs = map (head &&& length) $ group xs + +encode''' xs = map ((,) <$> head <*> length) $ group xs diff --git a/h11.hs b/h11.hs new file mode 100644 index 0000000..77b7295 --- /dev/null +++ b/h11.hs @@ -0,0 +1,15 @@ +import Control.Arrow +import Data.List + +data ListItem a = Single a | Multiple a Int + deriving (Show) + +encode :: Eq a => [a] -> [(a,Int)] +encode xs = map (head &&& length) $ group xs + +encodeModified :: Eq a => [a] -> [ListItem a] +encodeModified = map helper . encode + where + helper (a,1) = Single a + helper (a,c) = Multiple a c + diff --git a/h2.hs b/h2.hs new file mode 100644 index 0000000..60693ef --- /dev/null +++ b/h2.hs @@ -0,0 +1,11 @@ +import Data.Foldable as F + +myButLast :: Foldable f => f a -> a + +myButLast = fst . F.foldl (\(a,b) x -> (b,x)) (err1, err2) + where + err1 = error "Empty list" + err2 = error "Not enough elements" + +mySafeButLast :: Foldable f => f a -> Maybe a +mySafeButLast = fst . F.foldl (\(a,b) x -> (b,Just x)) (Nothing, Nothing) diff --git a/h3.hs b/h3.hs new file mode 100644 index 0000000..7c8604d --- /dev/null +++ b/h3.hs @@ -0,0 +1,7 @@ +elementAt :: [a] -> Integer -> Maybe a + +elementAt [] _ = Nothing +elementAt (x:xs) 1 = Just x +elementAt (x:xs) n + | n>1 = elementAt xs (n-1) + | otherwise = Nothing diff --git a/h4.hs b/h4.hs new file mode 100644 index 0000000..1aea629 --- /dev/null +++ b/h4.hs @@ -0,0 +1,3 @@ +myLength :: [a] -> Integer + +myLength = foldl (\x _ -> x+1) 0 diff --git a/h5.hs b/h5.hs new file mode 100644 index 0000000..720eb9f --- /dev/null +++ b/h5.hs @@ -0,0 +1,3 @@ +myReverse :: [a] -> [a] + +myReverse = foldl (flip $ (:)) [] diff --git a/h6.hs b/h6.hs new file mode 100644 index 0000000..dc643eb --- /dev/null +++ b/h6.hs @@ -0,0 +1,11 @@ +import Control.Monad +import Control.Applicative + +isPalindrome :: Eq a => [a] -> Bool +isPalindrome xs = xs == reverse xs + +isPalindrome' :: (Eq a) => [a] -> Bool +isPalindrome' = liftM2 (==) id reverse + +isPalindrome'' :: (Eq a) => [a] -> Bool +isPalindrome'' = (==) <*> reverse diff --git a/h7.hs b/h7.hs new file mode 100644 index 0000000..c67b5c5 --- /dev/null +++ b/h7.hs @@ -0,0 +1,5 @@ +data NestedList a = Elem a | List [NestedList a] + +myFlatten :: NestedList a -> [a] +myFlatten (Elem x) = [x] +myFlatten (List x) = concatMap myFlatten x diff --git a/h8.hs b/h8.hs new file mode 100644 index 0000000..219bc93 --- /dev/null +++ b/h8.hs @@ -0,0 +1,12 @@ +compress :: (Eq a) => [a] -> [a] +compress = foldr elim [] + where elim e [] = [e] + elim e ns + | e == head ns = ns + | otherwise = e:ns + +compress' xs = foldr f (const []) xs Nothing + where + f x r a@(Just q) + | x == q = r a + f x r _ = x : r (Just x) diff --git a/h9.hs b/h9.hs new file mode 100644 index 0000000..b190491 --- /dev/null +++ b/h9.hs @@ -0,0 +1,13 @@ +pack :: (Eq a) => [a] -> [[a]] +pack = foldr elim [] + where elim e [] = [[e]] + elim e p@(n:ns) + | e == head n = (e:n):ns + | otherwise = [e]:p + +pack' (x:xs) = let (first,rest) = span (==x) xs + in (x:first) : pack' rest +pack' [] = [] + +pack'' (x:xs) = (x:takeWhile (==x) xs):(pack'' $ dropWhile (==x) xs) +pack'' [] = [] diff --git a/p1.hs b/p1.hs deleted file mode 100644 index f0216c2..0000000 --- a/p1.hs +++ /dev/null @@ -1,9 +0,0 @@ - -myLast :: [a] -> a -myLast [] = error "Empty List" -myLast [x] = x -myLast (x:xs) = myLast xs - -myLast' = foldl1 (flip const) - -myLast'' = foldl1 (curry snd) diff --git a/p10.hs b/p10.hs deleted file mode 100644 index 68ffade..0000000 --- a/p10.hs +++ /dev/null @@ -1,20 +0,0 @@ -import Control.Applicative -import Control.Arrow -import Data.List - -encode :: Eq a => [a] -> [(a,Int)] -encode = foldr elim [] - where - elim e [] = [(e,1)] - elim e p@(n:ns) - | e == fst n = (e,1 + (snd n)):ns - | otherwise = (e,1):p - -encode' (x:xs) = let (first,rest) = span (==x) xs - in (x,1+(length first)) : encode' rest - -encode' [] = [] - -encode'' xs = map (head &&& length) $ group xs - -encode''' xs = map ((,) <$> head <*> length) $ group xs diff --git a/p11.hs b/p11.hs deleted file mode 100644 index 77b7295..0000000 --- a/p11.hs +++ /dev/null @@ -1,15 +0,0 @@ -import Control.Arrow -import Data.List - -data ListItem a = Single a | Multiple a Int - deriving (Show) - -encode :: Eq a => [a] -> [(a,Int)] -encode xs = map (head &&& length) $ group xs - -encodeModified :: Eq a => [a] -> [ListItem a] -encodeModified = map helper . encode - where - helper (a,1) = Single a - helper (a,c) = Multiple a c - diff --git a/p2.hs b/p2.hs deleted file mode 100644 index 60693ef..0000000 --- a/p2.hs +++ /dev/null @@ -1,11 +0,0 @@ -import Data.Foldable as F - -myButLast :: Foldable f => f a -> a - -myButLast = fst . F.foldl (\(a,b) x -> (b,x)) (err1, err2) - where - err1 = error "Empty list" - err2 = error "Not enough elements" - -mySafeButLast :: Foldable f => f a -> Maybe a -mySafeButLast = fst . F.foldl (\(a,b) x -> (b,Just x)) (Nothing, Nothing) diff --git a/p3.hs b/p3.hs deleted file mode 100644 index 7c8604d..0000000 --- a/p3.hs +++ /dev/null @@ -1,7 +0,0 @@ -elementAt :: [a] -> Integer -> Maybe a - -elementAt [] _ = Nothing -elementAt (x:xs) 1 = Just x -elementAt (x:xs) n - | n>1 = elementAt xs (n-1) - | otherwise = Nothing diff --git a/p4.hs b/p4.hs deleted file mode 100644 index 1aea629..0000000 --- a/p4.hs +++ /dev/null @@ -1,3 +0,0 @@ -myLength :: [a] -> Integer - -myLength = foldl (\x _ -> x+1) 0 diff --git a/p5.hs b/p5.hs deleted file mode 100644 index 720eb9f..0000000 --- a/p5.hs +++ /dev/null @@ -1,3 +0,0 @@ -myReverse :: [a] -> [a] - -myReverse = foldl (flip $ (:)) [] diff --git a/p6.hs b/p6.hs deleted file mode 100644 index dc643eb..0000000 --- a/p6.hs +++ /dev/null @@ -1,11 +0,0 @@ -import Control.Monad -import Control.Applicative - -isPalindrome :: Eq a => [a] -> Bool -isPalindrome xs = xs == reverse xs - -isPalindrome' :: (Eq a) => [a] -> Bool -isPalindrome' = liftM2 (==) id reverse - -isPalindrome'' :: (Eq a) => [a] -> Bool -isPalindrome'' = (==) <*> reverse diff --git a/p7.hs b/p7.hs deleted file mode 100644 index c67b5c5..0000000 --- a/p7.hs +++ /dev/null @@ -1,5 +0,0 @@ -data NestedList a = Elem a | List [NestedList a] - -myFlatten :: NestedList a -> [a] -myFlatten (Elem x) = [x] -myFlatten (List x) = concatMap myFlatten x diff --git a/p8.hs b/p8.hs deleted file mode 100644 index 219bc93..0000000 --- a/p8.hs +++ /dev/null @@ -1,12 +0,0 @@ -compress :: (Eq a) => [a] -> [a] -compress = foldr elim [] - where elim e [] = [e] - elim e ns - | e == head ns = ns - | otherwise = e:ns - -compress' xs = foldr f (const []) xs Nothing - where - f x r a@(Just q) - | x == q = r a - f x r _ = x : r (Just x) diff --git a/p9.hs b/p9.hs deleted file mode 100644 index b190491..0000000 --- a/p9.hs +++ /dev/null @@ -1,13 +0,0 @@ -pack :: (Eq a) => [a] -> [[a]] -pack = foldr elim [] - where elim e [] = [[e]] - elim e p@(n:ns) - | e == head n = (e:n):ns - | otherwise = [e]:p - -pack' (x:xs) = let (first,rest) = span (==x) xs - in (x:first) : pack' rest -pack' [] = [] - -pack'' (x:xs) = (x:takeWhile (==x) xs):(pack'' $ dropWhile (==x) xs) -pack'' [] = [] -- cgit v1.2.3-70-g09d2