'How to find vector length of each string element in a given vector

Am new to SICP and am writing a function length-of-vector-elements that takes such a vector as an argument and returns a vector of lengths

Here is so far what i have tried but not giving the results

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

The desired output is: (length-of-vector-elements colors) => #(3 6 6 5 4 6 6)



Solution 1:[1]

Elements of colors are strings, not vectors, so you have to use 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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Půda