[plt-scheme] input from file

From: Markus Spath (mspath at arcor.de)
Date: Sat Apr 3 06:26:07 EST 2004


I need some help reading data from a file.

currently I'm hardcoding data-strucures I want to deal with:

;; struct question
;; name is a symbol; v1, v2 are numbers
(define-struct question (name v1 v2))

;; questions-hardcoded: -> list of questions
;; generates a list of questions
(define (questions-hardcoded)
    (make-question 'q1 10 20)
    (make-question 'q2 5 13)
    (make-question 'q3 5 8)

(define questions (questions-hardcoded))

It would be nice to have a function that builds a list of questions from 
a file, like:

;; questions-from-file: string -> list of questions
;; generates a list of questions from a file
(define (questions-from-file filename)
   ... )

(define questions2 (questions-from-file "C:\\home\\scheme\\data.txt"))

q1 10 20
q2 5 13
q3 5 8

I've just started recently to teach myself scheme via htdp/DrScheme and 
all examples for I/O I found in the help-desk or via google use 
syntactic constructs I don't know yet; an idiot-proof and minimalistic 
example of how to read a file line by line and how to tokenize a string 
(at whitespace) into symbols and/or numbers would be of great help.


Posted on the users mailing list.