Debian Codegraph @

src/Data/List/Scans/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- List scans: definitions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Scans.Base where
10
11 open import Data.List.Base as List using (List; []; _∷_)
12 open import Data.List.NonEmpty.Base as List⁺ using (List⁺; _∷_; toList)
13 open import Function.Base using (_∘_)
14 open import Level using (Level)
15
16 private
17 variable
18 a b : Level
19 A : Set a
20 B : Set b
21
22
23 ------------------------------------------------------------------------
24 -- Definitions
25
26 -- Scanr
27
28 module _ (f : A → B → B) where
29
30 scanr⁺ : (e : B) → List A → List⁺ B
31 scanr⁺ e [] = e ∷ []
32 scanr⁺ e (x ∷ xs) = let y ∷ ys = scanr⁺ e xs in f x y ∷ y ∷ ys
33
34 scanr : (e : B) → List A → List B
35 scanr e = toList ∘ scanr⁺ e
36
37 -- Scanl
38
39 module _ (f : A → B → A) where
40
41 scanl⁺ : A → List B → List⁺ A
42 scanl⁺ e xs = e ∷ go e xs
43 where
44 go : A → List B → List A
45 go _ [] = []
46 go e (x ∷ xs) = let fex = f e x in fex ∷ go fex xs
47
48 scanl : A → List B → List A
49 scanl e = toList ∘ scanl⁺ e