disaster999
disaster999

Reputation: 97

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

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 :/

Upvotes: 1

Views: 48

Answers (1)

Óscar López
Óscar López

Reputation: 235994

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

Upvotes: 2

Related Questions