schemelispracketr5rs

How to 'display' multiple parameters in R5RS Scheme


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
)

Solution

  • Simplest:

    (define (display-all . vs)
      (for-each display vs))
    

    Note the use of for-each instead of map - 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 (using map with display would return a list of voids) 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:

    (define (insert-between v xs)
      (cond ((null? xs) xs)
            ((null? (cdr xs)) xs)
            (else (cons (car xs)
                        (cons v (insert-between v (cdr xs)))))))
    
    (define (display-all . vs)
      (for-each display (insert-between " " vs)))
    

    Now calling this:

    (display-all "blah" 4 "bloo")
    

    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:

    (define (display-all sep . vs)
      (for-each display (insert-between sep vs)))
    

    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.