代码之家  ›  专栏  ›  技术社区  ›  Mugoya Dihfahsih

如何找到给定向量中每个字符串元素的向量长度

  •  0
  • Mugoya Dihfahsih  · 技术社区  · 3 年前

    我是SICP新手,正在编写一个向量元素长度函数,该函数将向量作为参数并返回长度向量

    以下是迄今为止我所做的尝试,但没有给出结果

    #lang racket
    (define colors
    (vector "red" "orange" "yellow" "green" "blue" "indigo" "violet"))
    
    (define (length-of-vector-elements vct)
      (vector-map vector-length vct))
    (length-of-vector-elements colors)
    

    所需输出为: (length-of-vector-elements colors) => #(3 6 6 5 4 6 6)

    0 回复  |  直到 3 年前
        1
  •  2
  •   Martin Půda    3 年前

    要素 colors 是字符串,不是向量,所以必须使用 string-length :

    #lang racket
    
    (define colors
      (vector "red" "orange" "yellow" "green" "blue" "indigo" "violet"))
    
    (define (length-of-vector-elements vct)
      (vector-map string-length vct))
    
    (length-of-vector-elements colors)
    

    => #(3 6 6 5 4 6 6)