14
data List (A : Set) : Set where
16
_::_ : A -> List A -> List A
18
{-# BUILTIN LIST List #-}
19
{-# BUILTIN NIL [] #-}
20
{-# BUILTIN CONS _::_ #-}
22
length : {A : Set} -> List A -> Nat
24
length (_ :: xs) = 1 + length xs
26
map : {A B : Set} -> (A -> B) -> List A -> List B
28
map f (x :: xs) = f x :: map f xs
30
_++_ : {A : Set} -> List A -> List A -> List A
32
(x :: xs) ++ ys = x :: (xs ++ ys)
34
zipWith : {A B C : Set} -> (A -> B -> C) -> List A -> List B -> List C
36
zipWith f (x :: xs) (y :: ys) = f x y :: zipWith f xs ys
37
zipWith f [] (_ :: _) = []
38
zipWith f (_ :: _) [] = []
40
foldr : {A B : Set} -> (A -> B -> B) -> B -> List A -> B
42
foldr f z (x :: xs) = f x (foldr f z xs)
44
foldl : {A B : Set} -> (B -> A -> B) -> B -> List A -> B
46
foldl f z (x :: xs) = foldl f (f z x) xs
48
replicate : {A : Set} -> Nat -> A -> List A
50
replicate (suc n) x = x :: replicate n x
52
iterate : {A : Set} -> Nat -> (A -> A) -> A -> List A
54
iterate (suc n) f x = x :: iterate n f (f x)
56
splitAt : {A : Set} -> Nat -> List A -> List A × List A
57
splitAt zero xs = < [] , xs >
58
splitAt (suc n) [] = < [] , [] >
59
splitAt (suc n) (x :: xs) = add x $ splitAt n xs
61
add : _ -> List _ × List _ -> List _ × List _
62
add x < ys , zs > = < x :: ys , zs >
64
reverse : {A : Set} -> List A -> List A
65
reverse xs = foldl (flip _::_) [] xs