Skip to content

Commit ef40c56

Browse files
Expand data/list (no documentation added)
1 parent b46d28c commit ef40c56

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

hackett-lib/hackett/data/list.rkt

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
(require hackett/data/maybe
44
hackett/private/prim)
55

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)
910

1011
(defn head : (∀ [a] {(List a) -> (Maybe a)})
1112
[[{x :: _}] (Just x)]
@@ -45,13 +46,34 @@
4546
(defn uncons! : (forall [a] {(List a) -> (Tuple a (List a))})
4647
[[xs] (from-maybe (error! "uncons!: empty list") (uncons xs))])
4748

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+
4854
(defn nil? : (forall [a] {(List a) -> Bool})
4955
[[Nil] True]
5056
[[_ ] False])
5157

5258
(def length : (forall [a] {(List a) -> Integer})
5359
(foldr (λ [_ acc] {acc + 1}) 0))
5460

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+
5577
(defn take : (∀ [a] {Integer -> (List a) -> (List a)})
5678
[[n {x :: xs}]
5779
(if {n == 0}
@@ -60,6 +82,14 @@
6082
[[_ Nil]
6183
Nil])
6284

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+
6393
(defn drop : (∀ [a] {Integer -> (List a) -> (List a)})
6494
[[n {x :: xs}]
6595
(if {n == 0}
@@ -68,10 +98,30 @@
6898
[[_ Nil]
6999
Nil])
70100

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+
71117
(defn filter : (∀ [a] {{a -> Bool} -> (List a) -> (List a)})
72118
[[f {x :: xs}] (let ([ys (filter f xs)]) (if (f x) {x :: ys} ys))]
73119
[[_ Nil ] Nil])
74120

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+
75125
(defn foldl : (∀ [a b] {{b -> a -> b} -> b -> (List a) -> b})
76126
[[f a {x :: xs}] (let ([b (f a x)]) {b seq (foldl f b xs)})]
77127
[[_ a Nil ] a])
@@ -89,13 +139,27 @@
89139
(def sum : {(List Integer) -> Integer}
90140
(foldl + 0))
91141

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+
92148
(defn repeat : (∀ [a] {a -> (List a)})
93149
[[x] (letrec ([xs {x :: xs}]) xs)])
94150

151+
(defn replicate : (∀ [a] {Integer -> a -> (List a)})
152+
[[n x] (if {n <= 0}
153+
Nil
154+
{x :: (replicate {n - 1} x)})])
155+
95156
(defn cycle! : (∀ [a] {(List a) -> (List a)})
96157
[[Nil] (error! "cycle!: empty list")]
97158
[[xs ] (letrec ([ys {xs ++ ys}]) ys)])
98159

160+
(def concat-map : (forall [a b] {{a -> (List b)} -> (List a) -> (List b)})
161+
=<<)
162+
99163
(def or : {(List Bool) -> Bool}
100164
(foldr || False))
101165

0 commit comments

Comments
 (0)