Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions as elements of a vector in racket

Tags:

racket

How do you represent elements of a vector which are functions/procedures in racket?

I would think it would be something like:

#(+ -)

But when I retrieve the elements I get the symbols '+ and '-.

like image 327
G4143 Avatar asked Mar 13 '23 14:03

G4143


1 Answers

The reason is that #( is a special syntax for reading array literals, not an operator that evaluates the content between parentheses. From the manual:

When the reader encounters a #(, #[, or #{, it starts parsing a vector; see Vectors for information on vectors. ... In read-syntax mode, each recursive read for vector elements is also in read-syntax mode, so that the wrapped vector’s elements are also wrapped as syntax objects, and the vector is immutable.

Examples: #(1 apple 3) reads equal to (vector 1 'apple 3)

So, you should use the explicit vector operator:

(define a (vector + -))

((vector-ref a 0) 2 3)  ; => 5
like image 158
Renzo Avatar answered Apr 30 '23 08:04

Renzo