PreludeList.agda 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. module PreludeList where
  2. open import AlonzoPrelude as Prelude
  3. open import PreludeNat
  4. infixr 50 _::_ _++_
  5. data List (A : Set) : Set where
  6. [] : List A
  7. _::_ : A -> List A -> List A
  8. {-# BUILTIN LIST List #-}
  9. {-# BUILTIN NIL [] #-}
  10. {-# BUILTIN CONS _::_ #-}
  11. [_] : {A : Set} -> A -> List A
  12. [ x ] = x :: []
  13. length : {A : Set} -> List A -> Nat
  14. length [] = 0
  15. length (_ :: xs) = 1 + length xs
  16. map : {A B : Set} -> (A -> B) -> List A -> List B
  17. map f [] = []
  18. map f (x :: xs) = f x :: map f xs
  19. _++_ : {A : Set} -> List A -> List A -> List A
  20. [] ++ ys = ys
  21. (x :: xs) ++ ys = x :: (xs ++ ys)
  22. zipWith : {A B C : Set} -> (A -> B -> C) -> List A -> List B -> List C
  23. zipWith f [] [] = []
  24. zipWith f (x :: xs) (y :: ys) = f x y :: zipWith f xs ys
  25. zipWith f [] (_ :: _) = []
  26. zipWith f (_ :: _) [] = []
  27. foldr : {A B : Set} -> (A -> B -> B) -> B -> List A -> B
  28. foldr f z [] = z
  29. foldr f z (x :: xs) = f x (foldr f z xs)
  30. foldl : {A B : Set} -> (B -> A -> B) -> B -> List A -> B
  31. foldl f z [] = z
  32. foldl f z (x :: xs) = foldl f (f z x) xs
  33. replicate : {A : Set} -> Nat -> A -> List A
  34. replicate zero x = []
  35. replicate (suc n) x = x :: replicate n x
  36. iterate : {A : Set} -> Nat -> (A -> A) -> A -> List A
  37. iterate zero f x = []
  38. iterate (suc n) f x = x :: iterate n f (f x)
  39. splitAt : {A : Set} -> Nat -> List A -> List A × List A
  40. splitAt zero xs = < [] , xs >
  41. splitAt (suc n) [] = < [] , [] >
  42. splitAt (suc n) (x :: xs) = add x $ splitAt n xs
  43. where
  44. add : _ -> List _ × List _ -> List _ × List _
  45. add x < ys , zs > = < x :: ys , zs >
  46. reverse : {A : Set} -> List A -> List A
  47. reverse xs = foldl (flip _::_) [] xs