on master help sign in
Merge pull request #122 from LeifW/monoid_instance_for_string

Add a Monoid instance for String.

commit47fb39ad8b38f075f062af519a6cb9867c72d74c authorEdwin Brady <edwin.brady@gmail.com> dateSun 9 Dec 2012, 09:43 (14 years ago) parent19ef9407e137a9
2 files changed+13−7
lib/Prelude/List.idr+1−1 lib/Prelude/Strings.idr+12−6
lib/Prelude/List.idr+1−1
@@ -9,7 +9,7 @@ import Prelude.Nat
99%access public
1010%default total
1111
12−infixr 7 ::
12+infixr 7 ::
1313
1414data List a
1515 = Nil
lib/Prelude/Strings.idr+12−6
@@ -38,10 +38,16 @@ pack [] = ""
3838pack (x :: xs) = strCons x (pack xs)
3939
4040instance Cast String (List Char) where
41− cast = unpack
41+ cast = unpack
4242
4343instance Cast (List Char) String where
44− cast = pack
44+ cast = pack
45+
46+instance Semigroup String where
47+ (<+>) = (++)
48+
49+instance Monoid String where
50+ neutral = ""
4551
4652span : (Char -> Bool) -> String -> (String, String)
4753span p xs with (strM xs)
@@ -85,18 +91,18 @@ lines : String -> List String
8591lines s = map pack $ words' $ unpack s
8692
8793partial
88−foldr1 : (a -> a -> a) -> List a -> a
94+foldr1 : (a -> a -> a) -> List a -> a
8995foldr1 f [x] = x
9096foldr1 f (x::xs) = f x (foldr1 f xs)
9197
9298%assert_total -- due to foldr1, but used safely
9399unwords' : List (List Char) -> List Char
94−unwords' [] = []
100+unwords' [] = []
95101unwords' ws = (foldr1 addSpace ws)
96102 where
97103 addSpace : List Char -> List Char -> List Char
98− addSpace w s = w ++ (' ' :: s)
99−
104+ addSpace w s = w ++ (' ' :: s)
105+
100106unwords : List String -> String
101107unwords = pack . unwords' . map unpack
102108