Typo
[packages/base.git] / tests / list001.hs
1 {-# LANGUAGE CPP #-}
2 module Main where
3
4 import Data.List
5 import Control.Exception
6 #if __GLASGOW_HASKELL__ < 705
7 import Prelude hiding (catch)
8 #endif
9
10 -- This module briefly tests all the functions in PrelList and a few
11 -- from List.
12
13 -- ToDo: test strictness properties.
14
15 main = do
16
17 -- head
18 print (head [1,2,3,4], head "a")
19 catch (print (head [] :: String)) (\(ErrorCall _) -> putStr "head []\n")
20
21 -- tail
22 print (tail [1,2,3,4], tail "a")
23 catch (print (tail [] :: String)) (\(ErrorCall _) -> putStr "tail []\n")
24
25 -- init
26 print (init [1,2,3,4], init "a")
27 catch (print (init [] :: String)) (\(ErrorCall _) -> putStr "init []\n")
28
29 -- last
30 print (last [1,2,3,4], last "a")
31 catch (print (last [] :: String)) (\(ErrorCall _) -> putStr "last []\n")
32
33 -- null
34 print [null [], null "abc"]
35
36 -- length
37 print (length [1..10])
38
39 -- foldl
40 print (foldl (+) 1 [1..10])
41
42 -- foldl1
43 print (foldl1 (+) [1..10])
44 catch (print (foldl1 (+) [] :: Int)) (\(ErrorCall _) -> putStr "foldl1 []\n")
45
46 -- scanl
47 print (scanl (+) 1 [1..10])
48
49 -- scanl1
50 print (scanl1 (+) [1..10])
51 print (scanl1 (+) [] :: [Int])
52
53 -- foldr1
54 print (foldr1 (+) [1..10])
55 catch (print (foldr1 (+) [] :: Int)) (\(ErrorCall _) -> putStr "foldr1 []\n")
56
57 -- scanr
58 print (scanr (+) 1 [1..10])
59
60 -- scanr1
61 print (scanr1 (+) [1..10])
62 print (scanr1 (+) [] :: [Int])
63
64 -- iterate
65 print (take 10 (cycle (take 4 (iterate (+1) 1))))
66
67 -- take
68 print (take 4 (repeat "x"), take 0 (repeat "x"), take 5 [1..4])
69 catch (print (take (-1) [1..10])) (\(ErrorCall _) -> putStr "take (-1)\n")
70
71 -- replicate
72 print [replicate 2 "abc", replicate 0 "abc", replicate 3 []]
73
74 -- drop
75 print [drop 5 [1..10], drop 0 [1..10], drop 5 [1..4]]
76 catch (print (drop (-1) [1..10])) (\(ErrorCall _) -> putStr "drop (-1)\n")
77
78 -- splitAt
79 print [splitAt 5 [1..10], splitAt 5 [1..4]]
80 catch (print (splitAt (-1) [1..10])) (\(ErrorCall _) -> putStr "splitAt (-1)\n")
81
82 -- scan
83 print (span (<5) [1..10])
84
85 -- break
86 print (break (<5) [1..10])
87
88 -- reverse
89 print [reverse [1..10], reverse []]
90
91 -- and
92 print [and [], and [True], and [False]]
93
94 -- or
95 print [or [], or [True], or [False]]
96
97 -- elem
98 print [elem 5 [1..10], elem 0 [1..10], elem 1 []]
99
100 -- notElem
101 print [notElem 5 [1..10], notElem 0 [1..10], notElem 1 []]
102
103 -- lookkup
104 print (lookup 4 (zip [1..10] (reverse [1..10])))
105
106 -- sum
107 print [sum [1..10], sum []]
108
109 -- product
110 print [product [1..10], product []]
111
112 -- maximum
113 print (maximum [1..10])
114 catch (print (maximum [] :: Int)) (\(ErrorCall _) -> putStr "maximum []\n")
115
116 -- minimum
117 print (minimum [1..10])
118 catch (print (minimum [] :: Int)) (\(ErrorCall _) -> putStr "minimum []\n")
119
120 -- concatMap
121 print (concatMap (:[]) [(1::Int)..10])
122
123 -- zip
124 print [zip [1] [2], zip [1] [], zip [] [2], zip [1..5] [2..6]]
125
126 -- zip3
127 print (zip3 [1,2] [3,4] [5,6])
128
129 -- zipWith
130 print [zipWith (+) [1,2] [3,4], zipWith (+) [1] [], zipWith (+) [] []]
131
132 -- unzip
133 print [unzip [(1,2),(3,4)], unzip []]
134
135 -- unzip3
136 print [unzip3 [(1,2,3),(3,4,5)], unzip3 []]
137
138 -- unlines
139 print (unlines (lines "a\nb\nc\n"), lines "", unlines [])
140
141 -- words
142 print (unwords (words "a b c d"), words "", unwords [])
143
144 -- deleteBy
145 print [deleteBy (==) 1 [0,1,1,2,3,4],
146 deleteBy (==) (error "deleteBy") []]
147
148 -- delete
149 print [delete 1 [0,1,1,2,3,4],
150 delete (error "delete") []]
151
152 -- (\\)
153 print [ [0,1,1,2,3,4] \\ [3,2,1],
154 [1,2,3,4] \\ [],
155 [] \\ [error "\\\\"] ]