create a new list in the same format as taken from text file

61 views Asked by At

I'm trying to create a list of the contents within a file, problem is that the list is flat, it does not include the newlines (\r\n).

#lang racket
(define my-list '())

(define (read-all-file file-to-read)
  (for/list ([i (file->lines file-to-read)]) (
  set! my-list (foldr cons (list i) my-list))))

my-list

now my output is for example:

"lalala" "lalalall" "lalal"

instead of:

"lalala" 
"lalalall" 
"lalal"

Does anyone know how I could fix this? I thought the way I was doing it would have worked.. this is strange :/

1

There are 1 answers

0
Óscar López On

The list is fine, it's just a matter of how you display its elements. If you want to show them with newlines, do as follows:

(display (string-join my-list "\n"))

Another alternative would be:

(for-each (lambda (e) (display e) (newline)) my-list)

Either way, it works as expected:

=> lalala
   lalalall
   lalal