-2

Im going through a book in Scheme that has me using the writeln function. It expects me to be able to give it a series of strings and variables that it will then output to the console like so. (writeln "the number is " 4 "!") with the expected output being the number is 4!.

However when running and looking up documentation writeln and similar functions like display only take two arguments the data and an output. Giveing writeln multipule arguments like "the number is ", 4 and "!" will cause an error.

How does one print the formatted string like the book is expecting?

I have tried... Quoting the expression giving to writeln. trying alternative functions like display.

FiveSeven
  • 23
  • 5
  • 1
    You have already asked this question: [Print formatted string with variables](https://stackoverflow.com/questions/75477034/print-formatted-string-with-variables) – Martin Půda Feb 22 '23 at 05:03
  • Please don't spam the site with duplicates of your previously asked questions. You might want to read about [ask]. – ad absurdum Feb 22 '23 at 06:16
  • You might get the answer you want if you said what the book is. – molbdnilo Feb 26 '23 at 07:49
  • yeah none of you helped, I asked chatGPT and it was way more helpful than anyone here on this site. Get bent. – FiveSeven Mar 28 '23 at 16:42

2 Answers2

0

Scheme is a programming language: if you don't have a procedure you need you can usually write one.

In this case I don't know what the arguments to writeln should be but I am going to say they should be

  • the destination;
  • and any number of things to write.

The way to specify this kind of argument list in Scheme is either (lambda things ...) or, if there are a fixed number of initial arguments followed by a 'rest' argument then (lambda (thing1 thing2 ... thingn . things) ...). The equivalent fancy define syntax is (define (f thing1 thing2 ... . things) ...).

So:

(define (writeln to . things)
  ...)

what does the body look like? Well, there are two cases:

  • if things is empty, write a newline and stop;
  • if things is not, display the first thing and then write the rest of the things.

Well, there's a classic 'impedence match' problem here: I'd like to use writeln to write the rest of the things but the way I've specified it it takes a variable number of arguments, not a list of things. You can use apply to get around this but that's horrible: a better approach is a little auxiliary function. The natural way to specify that here is named let: (let name (...) ...) defines name as a procedure which can be called within its body.

So, here is the final thing:

(define (writeln to . things)
  (let write-things ((remaining things))
    (if (null? remaining)
        (newline to)
        (begin
          (display (car remaining) to)
          (write-things (cdr remaining))))))

And

> (writeln (current-output-port) 1 " " 2 " " 3)
1 2 3

This should work in R5RS Schemes but I have only tested it in Racket's R5RS language.

ignis volens
  • 7,040
  • 2
  • 12
-2

(load-option 'format) (define x 5) (write (format #T "The value of x is ~a" x))

FiveSeven
  • 23
  • 5
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 22 '23 at 03:03
  • 1
    Answers should usually be more than just code; they should provide some explanation. Consider reading [answer] for some pointers. Also note that `format` is not a standard Scheme procedure, so it does not provide a general solution. `format` is an implementation-specific procedure. – ad absurdum Feb 22 '23 at 06:20
  • `load-option` isn't described in any of the RNRS-reports so you are answering a Scheme question with a solution in a derived language or implementation without even mentioning the implementation this is supposed to work in. OP doesn't specify implementation so probably wants a portable answer. – Sylwester Feb 22 '23 at 23:50