write list to file using display-lines-to-file

539 views Asked by At

I'm having problems getting the display-lines-to-file working, here is what I have tried:

(define (list-to-file lst file) (display-lines-to-file lst file
                               #:exists 'replace
                               #:separator newline #"\n"))

(list-to-file myList "myfile.txt")

myList is a list of data that was read from another file, after processing that data I need to write the contents of the list (including newline characters) to myfile.txt

1

There are 1 answers

0
Greg Hendershott On BEST ANSWER

Racket keyword arguments have a #:keyword followed by a single value. The problem seems to be:

#:separator newline #"\n"

Instead you want:

#:separator #"\n"

I'd suggest a few other changes:

  • Since the default for #:separator is already #"\n", omit that.
  • You may want to add #:mode 'text so that \n is automatically translated to \r\n on Windows.
  • Racket allows almost anything in an identifier. Taking advantage of that, by convention "->" is used instead of "-to-" or "-2-".

Combining all that:

(define (list->file lst file)
  (display-lines-to-file lst
                         file
                         #:exists 'replace
                         #:mode 'text))