on master help sign in
Merge pull request #122 from LeifW/monoid_instance_for_string
47fb39aEdwin Brady
lib/Prelude/List.idr+1−1
@@ -9,7 +9,7 @@ import Prelude.Nat
9%access public
9%access public
10%default total
10%default total
11
11
12−infixr 7 ::
12+infixr 7 ::
13
13
14data List a
14data List a
15 = Nil
15 = Nil
lib/Prelude/Strings.idr+12−6
@@ -38,10 +38,16 @@ pack [] = ""
38pack (x :: xs) = strCons x (pack xs)
38pack (x :: xs) = strCons x (pack xs)
39
39
40instance Cast String (List Char) where
40instance Cast String (List Char) where
41− cast = unpack
41+ cast = unpack
42
42
43instance Cast (List Char) String where
43instance 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 = ""
45
51
46span : (Char -> Bool) -> String -> (String, String)
52span : (Char -> Bool) -> String -> (String, String)
47span p xs with (strM xs)
53span p xs with (strM xs)
@@ -85,18 +91,18 @@ lines : String -> List String
85lines s = map pack $ words' $ unpack s
91lines s = map pack $ words' $ unpack s
86
92
87partial
93partial
88−foldr1 : (a -> a -> a) -> List a -> a
94+foldr1 : (a -> a -> a) -> List a -> a
89foldr1 f [x] = x
95foldr1 f [x] = x
90foldr1 f (x::xs) = f x (foldr1 f xs)
96foldr1 f (x::xs) = f x (foldr1 f xs)
91
97
92%assert_total -- due to foldr1, but used safely
98%assert_total -- due to foldr1, but used safely
93unwords' : List (List Char) -> List Char
99unwords' : List (List Char) -> List Char
94−unwords' [] = []
100+unwords' [] = []
95unwords' ws = (foldr1 addSpace ws)
101unwords' ws = (foldr1 addSpace ws)
96 where
102 where
97 addSpace : List Char -> List Char -> List Char
103 addSpace : List Char -> List Char -> List Char
98− addSpace w s = w ++ (' ' :: s)
104+ addSpace w s = w ++ (' ' :: s)
99−
105+
100unwords : List String -> String
106unwords : List String -> String
101unwords = pack . unwords' . map unpack
107unwords = pack . unwords' . map unpack
102
108