From 11a5fd38018498a7b7feb0e392ac36003b0440ac Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 16 Mar 2015 23:10:05 +0800 Subject: +12 +13 +14 +15 --- h13.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 h13.hs (limited to 'h13.hs') diff --git a/h13.hs b/h13.hs new file mode 100644 index 0000000..ec0a395 --- /dev/null +++ b/h13.hs @@ -0,0 +1,15 @@ +data ListItem a = Single a | Multiple a Int + deriving (Show) + +encode :: Eq a => [a] -> [(a,Int)] +encode = foldr encodeHelper [] + where + encodeHelper a p@((x,c):ps) | x==a = (a,c+1):ps + encodeHelper a p = (a,1):p + +encodeDirect :: Eq a => [a] -> [ListItem a] +encodeDirect = map encodeHelper . encode + where + encodeHelper (a,c) + | c>1 = Multiple a c + | otherwise = Single a -- cgit v1.2.3-70-g09d2