File: ListBench.dhall

package info (click to toggle)
haskell-dhall 1.42.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,784 kB
  • sloc: haskell: 24,804; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 2,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

let iterate
    =   λ(n : Natural)
      → λ(a : Type)
      → λ(f : a → a)
      → λ(x : a)
      → List/build
        a
        (   λ(list : Type)
          → λ(cons : a → list → list)
          → List/fold
            { index : Natural, value : {} }
            ( List/indexed
              {}
              ( List/build
                {}
                (   λ(list : Type)
                  → λ(cons : {} → list → list)
                  → Natural/fold n list (cons {=})
                )
              )
            )
            list
            (   λ(y : { index : Natural, value : {} })
              → cons (Natural/fold y.index a f x)
            )
        )

let countTo =
  λ (x : Natural)
  → iterate x Natural (λ (x : Natural) → x + 1) 0

let sum =
  λ (xs : List Natural)
  → List/fold Natural xs Natural (λ (x : Natural) → λ (acc : Natural) → x + acc) 0


let map
        : ∀(a : Type) → ∀(b : Type) → (a → b) → List a → List b
        =   λ(a : Type)
          → λ(b : Type)
          → λ(f : a → b)
          → λ(xs : List a)
          → List/build
            b
            (   λ(list : Type)
              → λ(cons : b → list → list)
              → List/fold a xs list (λ(x : a) → cons (f x))
            )

let any
        : ∀(a : Type) → (a → Bool) → List a → Bool
        =   λ(a : Type)
          → λ(f : a → Bool)
          → λ(xs : List a)
          → List/fold a xs Bool (λ(x : a) → λ(r : Bool) → f x || r) False

let filter
        : ∀(a : Type) → (a → Bool) → List a → List a
        =   λ(a : Type)
          → λ(f : a → Bool)
          → λ(xs : List a)
          → List/build
            a
            (   λ(list : Type)
              → λ(cons : a → list → list)
              → List/fold
                a
                xs
                list
                (λ(x : a) → λ(xs : list) → if f x then cons x xs else xs)
            )

in sum (filter Natural Natural/even
     (map Natural Natural (λ(x:Natural) → x + 10) (countTo 1000)))