Commit 1aa56b6c authored by Michael Hanus 's avatar Michael Hanus

Operation length simplified, chr made total, more Show instances for tuples

parent 5df0278b
......@@ -331,15 +331,18 @@ null (_:_) = False
(x:xs) ++ ys = x : xs++ys
--- Computes the length of a list.
--length :: [_] -> Int
--length [] = 0
--length (_:xs) = 1 + length xs
length :: [_] -> Int
length [] = 0
length (_:xs) = 1 + length xs
{-
-- This version is more efficient but less usable for verification:
length :: [_] -> Int
length xs = len xs 0
where
len [] n = n
len (_:ys) n = let np1 = n + 1 in len ys $!! np1
-}
--- List index (subscript) operator, head has index 0.
(!!) :: [a] -> Int -> a
......@@ -508,8 +511,8 @@ words s = let s1 = dropWhile isSpace s
--- Concatenates a list of strings with a blank between two strings.
unwords :: [String] -> String
unwords ws = if ws==[] then []
else foldr1 (\w s -> w ++ ' ':s) ws
unwords ws = if null ws then []
else foldr1 (\w s -> w ++ ' ':s) ws
--- Reverses the order of all elements in a list.
reverse :: [a] -> [a]
......@@ -573,11 +576,13 @@ ord c = prim_ord $# c
prim_ord :: Char -> Int
prim_ord external
--- Converts a Unicode value into a character, fails iff the value is out of bounds
--- Converts a Unicode value into a character.
--- The conversion is total, i.e., for out-of-bound values, the smallest
--- or largest character is generated.
chr :: Int -> Char
chr n | n >= 0 = prim_chr $# n
-- chr n | n < 0 || n > 1114111 = failed
-- | otherwise = prim_chr $# n
chr n | n < 0 = prim_chr 0
| n > 1114111 = prim_chr 1114111
| otherwise = prim_chr $# n
prim_chr :: Int -> Char
prim_chr external
......@@ -1200,7 +1205,24 @@ instance (Show a, Show b, Show c, Show d) => Show (a, b, c, d) where
showsPrec _ (a, b, c, d) = showTuple [shows a, shows b, shows c, shows d]
instance (Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e) where
showsPrec _ (a, b, c, d, e) = showTuple [shows a, shows b, shows c, shows d, shows e]
showsPrec _ (a, b, c, d, e) =
showTuple [shows a, shows b, shows c, shows d, shows e]
instance (Show a, Show b, Show c, Show d, Show e, Show f)
=> Show (a, b, c, d, e, f) where
showsPrec _ (a, b, c, d, e, f) =
showTuple [shows a, shows b, shows c, shows d, shows e, shows f]
instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g)
=> Show (a, b, c, d, e, f, g) where
showsPrec _ (a, b, c, d, e, f, g) =
showTuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g]
instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h)
=> Show (a, b, c, d, e, f, g, h) where
showsPrec _ (a, b, c, d, e, f, g, h) =
showTuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g
,shows h]
instance Show a => Show [a] where
showsPrec _ = showList
......@@ -1378,12 +1400,11 @@ instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
lex :: ReadS String
lex xs = case xs of
"" -> [("","")]
(c:cs)
| isSpace c -> lex $ dropWhile isSpace cs
('\'':s) ->
[('\'' : ch ++ "'", t) | (ch, '\'' : t) <- lexLitChar s, ch /= "'"]
('"':s) -> [('"' : str, t) | (str, t) <- lexString s]
(c:cs)
| isSpace c -> lex $ dropWhile isSpace cs
| isSingle c -> [([c], cs)]
| isSym c -> [(c : sym, t) | (sym, t) <- [span isSym cs]]
| isAlpha c -> [(c : nam, t) | (nam, t) <- [span isIdChar cs]]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment