|
3 | 3 | (require hackett/data/maybe |
4 | 4 | hackett/private/prim) |
5 | 5 |
|
6 | | -(provide (data List) head last tail init head! last! tail! init! uncons uncons! nil? length take drop |
7 | | - filter foldr foldl reverse zip-with zip sum repeat cycle! or and any? all? elem? not-elem? |
8 | | - delete delete-by intersperse) |
| 6 | +(provide (data List) head last tail init head! last! tail! init! uncons uncons! unfoldr nil? length |
| 7 | + nth nth! find-index index-of take take-while drop drop-while tails inits filter find |
| 8 | + foldr foldl reverse zip-with zip sum product iterate repeat replicate cycle! concat-map |
| 9 | + or and any? all? elem? not-elem? delete delete-by intersperse) |
9 | 10 |
|
10 | 11 | (defn head : (∀ [a] {(List a) -> (Maybe a)}) |
11 | 12 | [[{x :: _}] (Just x)] |
|
45 | 46 | (defn uncons! : (forall [a] {(List a) -> (Tuple a (List a))}) |
46 | 47 | [[xs] (from-maybe (error! "uncons!: empty list") (uncons xs))]) |
47 | 48 |
|
| 49 | +(defn unfoldr : (forall [a b] {{b -> (Maybe (Tuple a b))} -> b -> (List a)}) |
| 50 | + [[step seed] (case (step seed) |
| 51 | + [Nothing Nil] |
| 52 | + [(Just (Tuple a b)) {a :: (unfoldr step b)}])]) |
| 53 | + |
48 | 54 | (defn nil? : (forall [a] {(List a) -> Bool}) |
49 | 55 | [[Nil] True] |
50 | 56 | [[_ ] False]) |
51 | 57 |
|
52 | 58 | (def length : (forall [a] {(List a) -> Integer}) |
53 | 59 | (foldr (λ [_ acc] {acc + 1}) 0)) |
54 | 60 |
|
| 61 | +(defn nth : (forall [a] {(List a) -> Integer -> (Maybe a)}) |
| 62 | + [[{x :: xs} n] (if {n < 0} Nothing |
| 63 | + (if {n == 0} (Just x) |
| 64 | + (nth xs {n - 1})))] |
| 65 | + [[Nil _] Nothing]) |
| 66 | + |
| 67 | +(defn nth! : (forall [a] {(List a) -> Integer -> a}) |
| 68 | + [[xs n] (from-maybe (error! "nth!: empty list") (nth xs n))]) |
| 69 | + |
| 70 | +(defn find-index : (forall [a] {{a -> Bool} -> (List a) -> (Maybe Integer)}) |
| 71 | + [[p {x :: xs}] (if (p x) (Just 0) (map (+ 1) (find-index p xs)))] |
| 72 | + [[_ Nil ] Nothing]) |
| 73 | + |
| 74 | +(def index-of : (forall [a] (Eq a) => {a -> (List a) -> (Maybe Integer)}) |
| 75 | + {find-index . ==}) |
| 76 | + |
55 | 77 | (defn take : (∀ [a] {Integer -> (List a) -> (List a)}) |
56 | 78 | [[n {x :: xs}] |
57 | 79 | (if {n == 0} |
|
60 | 82 | [[_ Nil] |
61 | 83 | Nil]) |
62 | 84 |
|
| 85 | +(defn take-while : (∀ [a] {{a -> Bool} -> (List a) -> (List a)}) |
| 86 | + [[p {x :: xs}] |
| 87 | + (if (p x) |
| 88 | + {x :: (take-while p xs)} |
| 89 | + Nil)] |
| 90 | + [[_ Nil] |
| 91 | + Nil]) |
| 92 | + |
63 | 93 | (defn drop : (∀ [a] {Integer -> (List a) -> (List a)}) |
64 | 94 | [[n {x :: xs}] |
65 | 95 | (if {n == 0} |
|
68 | 98 | [[_ Nil] |
69 | 99 | Nil]) |
70 | 100 |
|
| 101 | +(defn drop-while : (∀ [a] {{a -> Bool} -> (List a) -> (List a)}) |
| 102 | + [[p {x :: xs}] |
| 103 | + (if (p x) |
| 104 | + (drop-while p xs) |
| 105 | + xs)] |
| 106 | + [[_ Nil] |
| 107 | + Nil]) |
| 108 | + |
| 109 | +(defn tails : (forall [a] {(List a) -> (List (List a))}) |
| 110 | + [[{x :: xs}] {{x :: xs} :: (tails xs)}] |
| 111 | + [[Nil ] Nil]) |
| 112 | + |
| 113 | +(defn inits : (forall [a] {(List a) -> (List (List a))}) |
| 114 | + [[{x :: xs}] {(init! {x :: xs}) :: (inits xs)}] |
| 115 | + [[Nil ] Nil]) |
| 116 | + |
71 | 117 | (defn filter : (∀ [a] {{a -> Bool} -> (List a) -> (List a)}) |
72 | 118 | [[f {x :: xs}] (let ([ys (filter f xs)]) (if (f x) {x :: ys} ys))] |
73 | 119 | [[_ Nil ] Nil]) |
74 | 120 |
|
| 121 | +(defn find : (forall [a] {{a -> Bool} -> (List a) -> (Maybe a)}) |
| 122 | + [[p {x :: xs}] (if (p x) (Just x) (find p xs))] |
| 123 | + [[p Nil ] Nothing]) |
| 124 | + |
75 | 125 | (defn foldl : (∀ [a b] {{b -> a -> b} -> b -> (List a) -> b}) |
76 | 126 | [[f a {x :: xs}] (let ([b (f a x)]) {b seq (foldl f b xs)})] |
77 | 127 | [[_ a Nil ] a]) |
|
89 | 139 | (def sum : {(List Integer) -> Integer} |
90 | 140 | (foldl + 0)) |
91 | 141 |
|
| 142 | +(def product : {(List Integer) -> Integer} |
| 143 | + (foldl * 1)) |
| 144 | + |
| 145 | +(defn iterate : (forall [a] {{a -> a} -> a -> (List a)}) |
| 146 | + [[f x] {(f x) :: (iterate f (f x))}]) |
| 147 | + |
92 | 148 | (defn repeat : (∀ [a] {a -> (List a)}) |
93 | 149 | [[x] (letrec ([xs {x :: xs}]) xs)]) |
94 | 150 |
|
| 151 | +(defn replicate : (∀ [a] {Integer -> a -> (List a)}) |
| 152 | + [[n x] (if {n <= 0} |
| 153 | + Nil |
| 154 | + {x :: (replicate {n - 1} x)})]) |
| 155 | + |
95 | 156 | (defn cycle! : (∀ [a] {(List a) -> (List a)}) |
96 | 157 | [[Nil] (error! "cycle!: empty list")] |
97 | 158 | [[xs ] (letrec ([ys {xs ++ ys}]) ys)]) |
98 | 159 |
|
| 160 | +(def concat-map : (forall [a b] {{a -> (List b)} -> (List a) -> (List b)}) |
| 161 | + =<<) |
| 162 | + |
99 | 163 | (def or : {(List Bool) -> Bool} |
100 | 164 | (foldr || False)) |
101 | 165 |
|
|
0 commit comments