In R5RS Scheme how do you display multiple parameters, with a single call? my implementation below works, but adds extra parentheses and spaces.
#!/usr/bin/env racket
#lang r5rs
(define (display-all . rest) (display rest))
(display-all "I " "have " "a " "lovely " "bunch " "of " "coconuts\n")
results in
owner@K53TA:~$ ./Template.ss
(I have a lovely bunch of coconuts
)
If all the arguments are string, just use apply and string-append. both are in r5rs
Simplest:
Note the use of
for-each
instead ofmap
-for-each
is the same thing but assumes you're only calling the function for side-effects, so instead of returning a list of results (usingmap
withdisplay
would return a list ofvoid
s) it just returns void.This can get annoying if you want to display non-string things and have spacing between them, for instance if you want (display-all 12 "bananas") to display the string "12 bananas" you have to manually turn the number into a string and add the space yourself. It would be easier to just add spaces in-between elements of the list:
Now calling this:
does what you'd expect. If you don't want the spaces inserted automatically, you can specify another argument as the separator object and use it however you need. Here's a version that accepts a separator object:
This approach would make more sense in a version of scheme that supports optional and keyword arguments however, so you could default it to either a space or the empty string and not interfere with the rest-args.