Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading