Home > Enterprise >  Does specifying the number of elements on defining a vector in Racket make the program use less time
Does specifying the number of elements on defining a vector in Racket make the program use less time

Time:07-15

For example, can this

#!/usr/bin/env racket
#lang racket/base 

(define vector-of-pleasures #6("eating" "cold showers" "swimming" "running" "pullups" "weighlifting"))
(for ((pleasure vector-of-pleasures)) (displayln pleasure))

be more performant because of the optional annotation than this

#!/usr/bin/env racket
#lang racket/base

(define vector-of-pleasures #("eating" "cold showers" "swimming" "running" "pullups" "weighlifting"))
(for ((pleasure vector-of-pleasures)) (displayln pleasure))

?

Methinks it should not matter since in both snippets vector-of-pleasures is immutable. What are, other than performance, possible reasons for annotating the number of the elements of the vector on its definition?

CodePudding user response:

No, it doesn't. The real use of that syntax is that it lets you write a vector of a bunch of identical things easily: #6(1) is a vector of 6 1s.

But I think it could save space. I can't see why, for instance:

(let ((v #2((1 2))))
  (eq? (vector-ref v 0) (vector-ref v 1)))

should not be true. But then this rapidly turns into the question of when similar literals can be folded: can (eq? '(1 2) '(1 2)) return true for instance? I don't know what Racket's rules are on that.

  • Related