[plt-scheme] input from file

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Apr 3 09:37:22 EST 2004

If you're willing to write your data in a slightly different format,
you can make the reading much simpler.


((q1 10 20)
 (q2 5 13)
 (q3 5 8))

;; read-data : -> list-of-questions
(define (read-data filename)
  (map read-line (call-with-input-file filename read)))

;; read-line : list of three elements -> question
(define (read-line l)
    (first l)
    (first (rest l))
    (first (rest (rest l)))))

For this to work, you need `map', which is in intermediate, and you
also need the functions call-with-input-file and read. They aren't in
the teaching languages, but save the code below in the file
read-from-file.ss and use it as a teachpack:

(module read-from-file mzscheme
  (provide read-from-file)
  (define (read-from-file fn) 
    (call-with-input-file fn read)))

In that case, read-data becomes:

;; read-data : -> list-of-questions
(define (read-data filename)
   (map read-line (read-from-file filename)))


Posted on the users mailing list.