Racket中的合同违规(make-array)

时间:2016-01-29 06:30:44

标签: racket

我想创建一个1xn的数组,其中n是某个输入数组中出现的最大值(它只包含整数)。 我用下面的示例输入数组检查了get-max的类型。它的类型是整数。 (注意,描述来自图书馆(planet williams / describe / describe)))

>> (describe (array-ref (array-axis-max A 0) #()))
6 is a byte (i.e., an exact positive integer fixnum between 0 and 255 inclusive) six

这是我生成数组的代码:

(define (array-from-max input)
  (local ((define get-max (array-ref (array-axis-max input 0) #())))
    (make-array #(get-max) 0)))

但是,以下调用会产生低于它的错误。 make-array需要一个整数但是给出了#get; max。

问题1:我是否真的将符号值传递给make-array?

问题2:如何成功将(array-axis-max)的结果传递给(make-array)的size参数?

问题3:#()中的内容是否被评估?它是如何评估的?

>> (array-from-max (array #[3 6 4 1 3 4 1 4]))
make-array: contract violation
  expected: Integer
  given: 'get-max
  in: an element of
      the 1st argument of
      (->
       (vectorof Integer)
       any/c
       (struct/c
        Array
        (vectorof Index)
        any/c
        (box/c (or/c #f #t))
        (-> any)
        (-> (vectorof Index) any)))

1 个答案:

答案 0 :(得分:1)

问题1:是的,就像杰克上面说的那样。 #就像字符串的"一样 - 一旦进入内部,字符就会逐字逐句。

问题2:使用(vector get-max)。这是一个例子

#lang racket
(require math/array)

(define (array-from-max input)
  (local ((define get-max (array-ref (array-axis-max input 0) #())))
      (make-array (vector get-max) 0)))

(array-from-max (array #[3 6 4 1 3 4 1 4]))

问题3:#()内的内容会自动引用,如“球拍指南”Vectors中所述