On this page:
3.10.1 数组定义
Array?
Array/  c
Array
3.10.2 构造新数组
array
list->array
repeat
range
empty
singleton
3.10.3 数组合并
+  +
:
<:  >
zip-with
zip
3.10.4 列表属性
length
empty?
3.10.5 子列表操作
head
tail
take-while
drop-while
take
take-right
drop
drop-right
filter
reject
3.10.6 数组查找
find
member?
3.10.7 数组转换
reverse
sort-by
sort
filter-map
partition
span
split-at
group-by
group
array-adjust
array-update
3.10.8 数组解构
array-get
array-index
foldl
foldr
3.10.9 数组特有语法
for/  array
8.12

3.10 数组🔗

azelf内置的数组,取代了racket的list。 整体接口偏向Haskell的Data.List。

3.10.1 数组定义🔗

procedure

(Array? a)  boolean?

  a : any/c
是不是数组。

procedure

(Array/c a)  contract?

  a : contract?
contract构造器。

syntax

(Array pattern ...)

数组模式匹配关键字。

Examples:
> (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?

Example:
> (match (array 1 2 3 4 5)
    [(Array xs ...) xs])

'(1 2 3 4 5)

3.10.2 构造新数组🔗

procedure

(array x ...)  (Array/c a)

  x : a
类似list,构造出新的Array?

Examples:
> (array)

(Array)

> (array 1 2 3)

(Array 1 2 3)

procedure

(list->array xs)  (Array/c a)

  xs : (listof a)
list转化成array

Example:
> (list->array (list 1 2 3))

(Array 1 2 3)

procedure

(repeat n a)  (Array/c a)

  n : (or/c zero? positive?)
  a : a

Example:
> (repeat 10 "hello")

(Array

 "hello"

 "hello"

 "hello"

 "hello"

 "hello"

 "hello"

 "hello"

 "hello"

 "hello"

 "hello")

procedure

(range start end)  (Array/c number?)

  start : number?
  end : number?

Example:
> (range 1 10)

(Array 1 2 3 4 5 6 7 8 9)

value

empty : (Array/c a)

空数组。

(= (array) empty)

procedure

(singleton x)  (Array/c a)

  x : a
生成一个单元素的数组。

(= (array x) (singleton x))
3.10.3 数组合并🔗

procedure

(++ xs ys)  (Array/c a)

  xs : (Array/c a)
  ys : (Array/c a)
数组拼接。

Example:
> (++ (array 1 2 3) (array 3 2 1))

(Array 1 2 3 3 2 1)

procedure

(: x xs)  (Array/c a)

  x : a
  xs : (Array/c a)
相当于cons

Examples:
> (: 1 (array))

(Array 1)

> (: 1 (array 2 3))

(Array 1 2 3)

procedure

(<:> x xs)  (Array/c a)

  x : a
  xs : (Array/c a)
往数组后面追加元素,相当于其他语言的push。

Examples:
> (<:> 1 (array))

(Array 1)

> (<:> 1 (array 2 3))

(Array 2 3 1)

procedure

(zip-with f xs ys)  (Array/c c)

  f : (-> a b c)
  xs : (Array/c a)
  ys : (Array/c b)
合并两条数组。

procedure

(zip xs ys)  (Array/c (cons/c a b))

  xs : (Array/c a)
  ys : (Array/c b)
合并两条数组。

3.10.4 列表属性🔗

procedure

(length xs)  exact-nonnegative-integer?

  xs : (Array/c any/c)
数组长度。

Examples:
> (length (array))

0

> (length (array 1 2 3))

3

procedure

(empty? xs)  boolean?

  xs : (Array/c any/c)
是否是空数组。

Examples:
> (empty? (array))

#t

> (empty? (array 1))

#f

3.10.5 子列表操作🔗

procedure

(head xs)  (Maybe/c a)

  xs : (Array/c a)
(tail xs)  (Maybe/c a)
  xs : (Array/c a)

Examples:
> (head (array))

(Nothing)

> (head (range 10 20))

(Just 10)

> (tail (array))

(Nothing)

> (tail (range 10 20))

(Just 19)

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)

Examples:
> (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

(take n xs)  (Array/c a)

  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)

Examples:
> (take 10 (array 1 2 3 4))

(Array 1 2 3 4)

> (drop -1 (array 1 2 3 4))

(Array)

procedure

(filter f xs)  (Array/c a)

  f : (-> a boolean?)
  xs : (Array/c a)
(reject f xs)  (Array/c a)
  f : (-> a boolean?)
  xs : (Array/c a)
过滤列表。

Examples:
> (filter even? (array 1 2 3 4))

(Array 2 4)

> (reject even? (array 1 2 3 4))

(Array 1 3)

3.10.6 数组查找🔗

procedure

(find f xs)  (Maybe/c a)

  f : (-> a boolean?)
  xs : (Array/c a)

Examples:
> (find (= 1) (array 3 1 2))

(Just 1)

> (find (= (Just 1)) (array nothing nothing (Just 2)))

(Nothing)

> (find (= nothing) (array nothing nothing (Just 2)))

(Just (Nothing))

procedure

(member? a xs)  (Maybe/c Eq?)

  a : Eq?
  xs : (Array/c Eq?)

Examples:
> (member? 1 (array))

#f

> (member? 2 (array 1 2 3))

#t

3.10.7 数组转换🔗

procedure

(reverse xs)  (Array/c a)

  xs : (Array/c a)
反转数组。

procedure

(sort-by f xs)  (Array/c a)

  f : (-> a a boolean?)
  xs : (Array/c a)
(sort xs)  (Array/c a)
  xs : (Array/c a)
sort默认升序排序。

(define sort (sort-by <))

procedure

(filter-map f xs)  (Array/c b)

  f : (-> a (Maybe/c b))
  xs : (Array/c a)
即要filter又要map

Examples:
> (define (f x)
    (if (< x 0)
        nothing
        (Just (add1 x))))
> (filter-map f (array -1 1 -2 2))

(Array 2 3)

procedure

(partition f xs)  (cons (Array/c a) (Array/c a))

  f : (-> a boolean?)
  xs : (Array/c a)
挑出两堆数组,左组满足f条件,右组不足。

procedure

(span f xs)  (cons/c (array a) (array a))

  f : (-> a boolean?)
  xs : (Array/c a)
从头寻找满足f的元素,直到未满足为止;返回一组满足元素的集合,及剩下元素集合。

Examples:
> (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

(split-at n xs)  (cons (Array/c a) (Array/c a))

  n : exact-nonnegative-integer?
  xs : (Array/c a)
按位置分割一个数组。

Examples:
> (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

(group-by f xs)  (Array/c (Array/c a))

  f : (-> a a boolean?)
  xs : (Array/c)
(group xs)  (Array/c (Array/c a))
  xs : (Array/c a)
按条件f分组。

procedure

(array-adjust f i xs)  (Array/c a)

  f : (-> a a)
  i : exact-nonnegative-integer
  xs : (Array/c a)
更新i元素。

Examples:
> (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)
更新i元素。

3.10.8 数组解构🔗

procedure

(array-get i xs)  (Maybe/c a)

  i : exact-nonnegative-integer?
  xs : (Array/c a)
获取第i - 1个元素。

Examples:
> (array-get 0 empty)

(Nothing)

> (array-get 10 (array 1 2 3))

(Nothing)

> (array-get 1 (array 1 2 3))

(Just 2)

procedure

(array-index a xs)  (Maybe/c exact-nonnegative-integer?)

  a : Eq?
  xs : (Array/c Eq?)
查看元素在哪个位置。

Examples:
> (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/listfor*/list,结果产生的是array

Examples:
> (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")