Panda
Panda

Reputation: 63

Command line processing in Racket contains embedded void

What about command-line processing in racket do I not understand? For example, I just want to process the first argument as foo.html. From the command-line I run it as:

racket cmd.rkt foo.html

Unfortunately that just returns:

foo.html'#(#<void>)

Here's the code for cmd.rkt:

(for/vector ([i (current-command-line-arguments)])
    (display i))

Upvotes: 3

Views: 266

Answers (2)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236112

Let's see what the code is doing. With this command...

racket cmd.rkt foo.html

... You're telling the interpreter: run cmd.rkt and pass a single parameter, the string "foo.html".

In the script, this code...

(for/vector ([i (current-command-line-arguments)])
    (display i))

...Is iterating over the command line arguments (a single one in the example), displaying each one in turn. Do notice that display returns #<void> as its value, and for/vector creates a vector with all the returned values in the iteration, so naturally this is the output:

foo.html'#(#<void>)

The first part, foo.html is the result of displaying the single command line argument that was passed. The second part, '#(#<void>) is a vector with a single element, #<void>, which as mentioned before, is the result of calling display.

Finally, as has been mentioned in the other answers: if you only intended to print all of the received command line arguments, then avoid using for/vector - you don't want to create a new vector, only traverse and print the arguments and a simple for will suffice. This should work, and includes @GregHendershott's recommended optimization regarding the use of in-vector:

(for ([i (in-vector (current-command-line-arguments))])
    (display i))

Upvotes: 3

jacobm
jacobm

Reputation: 14035

for/vector isn't called that because it iterates over vectors, it's called that because it accumulates the results of its body expression into a vector. So for each commandline argument it evaluates the display call, which prints the argument and returns #<void>, and accumulates the result into a vector of void values.

Use for instead and the problem will go away.

Upvotes: 4

Related Questions