3.10 数组
azelf内置的数组,取代了racket的list。 整体接口偏向Haskell的Data.List。
3.10.1 数组定义
syntax
(Array pattern ...)
> (define/match1 f [(Array) 'empty] [(Array _) 'one] [(Array _ _) 'two] [(Array a b _ ...) (+ a b)] [_ 'fucked]) > (f (array)) 'empty
> (f (array 1)) 'one
> (f (array 1 2)) 'two
> (f (array 1 2 3 4)) 3
这里需要注意,...匹配出来的数组结构是list,不是Array?。
3.10.2 构造新数组
procedure
(list->array xs) → (Array/c a)
xs : (listof a)
> (list->array (list 1 2 3)) (Array 1 2 3)
> (repeat 10 "hello")
(Array
"hello"
"hello"
"hello"
"hello"
"hello"
"hello"
"hello"
"hello"
"hello"
"hello")
> (range 1 10) (Array 1 2 3 4 5 6 7 8 9)
(= (array) empty)
(= (array x) (singleton x))
3.10.3 数组合并
3.10.4 列表属性
procedure
(length xs) → exact-nonnegative-integer?
xs : (Array/c any/c)
3.10.5 子列表操作
procedure
(take-while f xs) → (Array/c a)
f : (-> a boolean?) xs : (Array/c a) (drop-while f xs) → (Array/c a) f : (-> a boolean?) xs : (Array/c a)
> (take-while even? (array 2 4 7 8 10)) (Array 2 4)
> (drop-while even? (array 2 4 7 8 10)) (Array 7 8 10)
procedure
n : exact-integer? xs : (Array/c a) (take-right n xs) → (Array/c a) n : exact-integer? xs : (Array/c a) (drop n xs) → (Array/c a) n : exact-integer? xs : (Array/c a) (drop-right n xs) → (Array/c a) n : exact-integer? xs : (Array/c a)
procedure
f : (-> a boolean?) xs : (Array/c a) (reject f xs) → (Array/c a) f : (-> a boolean?) xs : (Array/c a)
3.10.6 数组查找
3.10.7 数组转换
(define sort (sort-by <))
procedure
(filter-map f xs) → (Array/c b)
f : (-> a (Maybe/c b)) xs : (Array/c a)
> (span (λ (x) (< x 3)) (array 1 2 3 4 1 2 3 4)) '(#<Array: 1 2> . #<Array: 3 4 1 2 3 4>)
> (span (λ (x) (< x 9)) (array 1 2 3)) '(#<Array: 1 2 3> . #<Array:>)
procedure
n : exact-nonnegative-integer? xs : (Array/c a)
> (split-at 0 (array 1 2 3)) '(#<Array:> . #<Array: 1 2 3>)
> (split-at 10 (array 1 2 3)) '(#<Array: 1 2 3> . #<Array:>)
> (split-at 2 (array 1 2 3)) '(#<Array: 1 2> . #<Array: 3>)
procedure
(array-adjust f i xs) → (Array/c a)
f : (-> a a) i : exact-nonnegative-integer xs : (Array/c a)
> (array-adjust add1 0 (array 1 2 3)) (Array 2 2 3)
> (array-adjust add1 1 (array)) (Array)
> (array-adjust add1 10 (array 1 2 3)) (Array 1 2 3)
procedure
(array-update i x xs) → (Array/c a)
i : exact-nonnegative-integer? x : a xs : (Array/c a)
3.10.8 数组解构
procedure
i : exact-nonnegative-integer? xs : (Array/c a)
procedure
(array-index a xs) → (Maybe/c exact-nonnegative-integer?)
a : Eq? xs : (Array/c Eq?)
> (array-index 'a empty) (Nothing)
> (array-index (Just "a") (array nothing (Just "b") (Just "a"))) (Just 2)
procedure
(foldl f acc xs) → a
f : (-> a b a) acc : a xs : (Array/c b) (foldr f acc xs) → a f : (-> b a a) acc : a xs : (Array/c b)
3.10.9 数组特有语法
syntax
(for/array (条件 ...) (代码体 ...))
(for*/array (条件 ...) (代码体 ...))
> (for/array ([n (in-range 1 10)]) (+ n n)) (Array 2 4 6 8 10 12 14 16 18)
> (for*/array ([n (in-range 1 10)] [m (array "hello" "world")]) (array n m))
(Array
1
"hello"
1
"world"
2
"hello"
2
"world"
3
"hello"
3
"world"
4
"hello"
4
"world"
5
"hello"
5
"world"
6
"hello"
6
"world"
7
"hello"
7
"world"
8
"hello"
8
"world"
9
"hello"
9
"world")