1
module Prelude.List where
3
open import Prelude.Bool
4
open import Prelude.Nat
5
open import Prelude.Fin
9
data List (A : Set) : Set where
11
_::_ : A -> List A -> List A
13
{-# BUILTIN LIST List #-}
14
{-# BUILTIN NIL [] #-}
15
{-# BUILTIN CONS _::_ #-}
19
_++_ : {A : Set} -> List A -> List A -> List A
21
(x :: xs) ++ ys = x :: (xs ++ ys)
23
snoc : {A : Set} -> List A -> A -> List A
25
snoc (x :: xs) e = x :: snoc xs e
27
length : {A : Set} -> List A -> Nat
29
length (x :: xs) = 1 + length xs
31
zipWith : ∀ {A B C} -> (A -> B -> C) -> List A -> List B -> List C
32
zipWith f (x :: xs) (y :: ys) = f x y :: zipWith f xs ys
35
map : ∀ {A B} -> (A -> B) -> List A -> List B
37
map f (x :: xs) = f x :: map f xs
39
mapIgen : ∀ {A B} -> (A -> B) -> List A -> List B
42
_!_ : ∀ {A} -> (xs : List A) -> Fin (length {A} xs) -> A
43
_!_ {A} (x :: xs) (fz .{length xs}) = x
44
_!_ {A} (x :: xs) (fs .{length xs} n) = _!_ {A} xs n
47
_[_]=_ : {A : Set} -> (xs : List A) -> Fin (length xs) -> A -> List A
48
(a :: as) [ fz ]= e = e :: as
49
(a :: as) [ fs n ]= e = a :: (as [ n ]= e)
52
listEq : {A : Set} -> (A -> A -> Bool) -> List A -> List A -> Bool
54
listEq _==_ (a :: as) (b :: bs) with a == b
55
... | true = listEq _==_ as bs
59
tail : {A : Set} -> List A -> List A
63
reverse : {A : Set} -> List A -> List A
65
reverse (x :: xs) = reverse xs ++ (x :: [])
67
init : {A : Set} -> List A -> List A
68
init xs = reverse (tail (reverse xs))
70
filter : {A : Set} -> (A -> Bool) -> List A -> List A
72
filter p (a :: as) with p a
73
... | true = a :: filter p as
74
... | false = filter p as