mrMike91
mrMike91

Reputation: 55

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

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

Upvotes: 2

Views: 532

Answers (1)

Greg Hendershott
Greg Hendershott

Reputation: 16250

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))

Upvotes: 1

Related Questions