views:

54

answers:

2

I've worked out how to read in the file and it works using the following code:

(define p (read(open-input-file "starbucks4.sxml")))

But how do i store p as a list with elements separated by \n characters.

Thanks.

A: 

IIRC, both MIT Scheme and Racket, as far as some other implementations, have functions call-with-input-file and call-with-output-file. See this for details of their usage and this for complete reference.

As for a \n separated list, the best approach I know is using Common Lisp style format function. But if your implementation does't have such function, you will need to manually break your list and print it using write function.

Andrei
Yes, you should use `call-with-input-file` or `with-input-from-file` to close the file when you're done reading it. As for your suggested use of `format` -- that is irrelevant here, `format` is used to assemble strings, not to deconstruct one.`
Eli Barzilay
As far as I understand, the question was about outputting elements of list, separated by `\n`. If this is true, you will need to format string anyway, and I don't know a better way to do it then CL `format` function. If it is available, of course.
Andrei
+1  A: 

If you're using Racket (or PLT Scheme), you can use the read-line function to read each line of the file to get them -- but they won't be read as s-exps, each will simply be a string.

configurator
Note also that in Racket you have a convenient [`file->lines`](http://docs.racket-lang.org/reference/Filesystem.html#%28def._%28%28lib._racket/file..rkt%29._file-%7e3elines%29%29) function.
Eli Barzilay