#!/bin/sh
string=? ; exec mzscheme -g -l core.ss -r $0 "$@"

(if (file-exists? "db.ss")
    (require "db.ss")
    (error 'reader "couldn't find db.ss"))
;; defines db-search : (list Y) -> X

;; the number of items that each line in the file must contain
(define ITEMS-PER-LINE 3)

;; read-file :  -> (list (list any))
;; effect:
;;   read lines from standard input and form list of converted lines
;;   use convert-line->record to create a record from a line 
(define (read-file)
  (let ((n (read-line)))
    (cond
      [(eof-object? n) empty]
      [else (cons (convert-line->record n) (read-file))])))

;; convert-line->record : str -> (list any)
;; convert a string into a record, using make-record
(define (convert-line->record str)
  (local (;; read string as file
	  (define ip (open-input-string str)) 
	  (define (L)
	    (let ((item (read ip)))
	      (cond
		[(eof-object? item) empty]
		[else (cons item (L))])))
	  (define line-as-list (L)))
    (cond
      [(= (length line-as-list) ITEMS-PER-LINE)
       (apply make-record line-as-list)]
      [else (error 'reader "bad line: ~e" str)])))

(printf "~a~n" (db-search (read-file)))
