---
title: Eboard 29  Files in Scheme
number: 29
section: eboards
held: 2017-11-06
---
CSC 151.03, Class 29:  Files in Scheme
======================================

_Overview_

* Preliminaries
    * Notes and news
    * Upcoming work
    * Extra credit
    * Questions
* Lab
* Debrief

### News / Etc.

* Sorry that I could not be in class on Friday.
* No, grading is not done.  Thursday, Friday, Saturday, and half of
  Sunday were booked.  I spent six hours grading, but that's not enough.
    * Tonight is booked with Wednesday's lab/reading and Friday's project
      and the CSC 301 exam.
    * So cross your fingers for exams back on Friday.
    * I'm sorry.

### Upcoming Work

* [Writeup for class 28](../writeups/writeup28) due TONIGHT at 10:30 p.m.
    * Exercise 5.  (No documentation necessary, but appreciated.)
    * To: <csc151-03-grader@grinnell.edu>
    * Subject: CSC 151.03 Writeup 28 (YOUR NAMES)
* [Writeup for class 29](../writeups/writeup29) due Wednesday at 10:30 p.m.
    * Exercise 6, no documentation required
    * To: <csc151-03-grader@grinnell.edu>
    * Subject: CSC 151.03 Writeup 29 (YOUR NAMES)
* Read [Analyzing Algorithms](../readings/analysis) for Wednesday's class.
    * No, it's not ready.
* [Exam 3](../exams/exam03) distributed.
    * Prologue due Friday.
    * Exam due Tuesday the 14th.
    * Cover pages due Wednesday the 15th.
    * Epilogues due Wednesday the 15th.

### Extra credit (Academic/Artistic)

* Leyla McCalla Trio, Nov. 6, 7:30 p.m. Herrick
* Animated Films, Tuesday, Nov. 7, 11:00 a.m., Faulconer
* CS Table (Computer-Aided Gerrymandering), Tuesday, Nov. 7, noon, 
  Day Dining Room
* Crip Technoscience, Disabled People as Makers and Knowers, Wednesday,
  Nov. 8, 4:15 p.m., JRC 101.

### Extra credit (Peer)

* Grinnell Review needs reviewers and pizza eaters.  Tuesday 5-7 in 
  some random art history room in Bucksbaum.  Follow the pizza smell.
* DACA talk today at 7pm.

### Extra credit (Misc)

* Pioneer Weekend.  Startup competition.
* ISO Food Bazaar.

### Other good things

* Drag Show.
* Collage making at the Stew Thursday 7-9.  Across from Bikes To You
  on Broad.

### Questions

Lab
---

When you get to problem 4 ...

* To prepare for writing output:
  `(define myport (open-output-file "Desktop/mydata"))` or
  `(define myport (open-output-file "/home/username/Desktop/mydata"))` 
* To add data to that file
  `(display data myport)` or `(write data myport)`.
* To add a newline to that file
  `(newline myport)`
* When you are done
  `(close-output-port myport)`

Note: As you've likely discovered, by default, DrRacket does not let
you overwrite existing files.  That's good.  You don't want to overwrite
something accidentally.

When you get to problem 5, you'll discover that

* Some parts of the DrRacket documentation are hard to read.
* You can write procedures with optional parameters
* Use `(open-output-file FILENAME #:exists 'replace)`

Your writeup is exercise 6 (no documentation necessary)

Debrief
-------

[Note: I have added more office hours for this week and next.]

### Answers to 1b

* You could open the file in DrRacket, type `(+ ` at the beginning, type
  `)` at the end, and click "Run"
* Open the file in a spreadsheet and use spreadsheet tools.
* `(apply + (map string->number (file->words FILE)))`

### Answers to 2

Start with the code for `sum-of-file`.

```
(define sum-of-file
  (lambda (file-name)
    (let ([source (open-input-file file-name)])
      (let kernel ([sum-so-far 0])
        (let ([nextval (read source)])
          (cond
            ; Are we at the end of the file?
            ; Then stop and return our running sum.
            [(eof-object? nextval) 
             (close-input-port source) 
             sum-so-far]
            ; Have we just read a number?
            ; If so, add it to the sum of the remaining numbers.
            [(number? nextval) 
             (kernel (+ nextval sum-so-far))]
            ; Hmmm ... not a number.  Skip it.
            [else 
             (kernel sum-so-far)]))))))
```

Update to use `read-char` and such.

```
(define sum-of-file
  (lambda (file-name)
    (let ([source (open-input-file file-name)])
      (let kernel ([sum-so-far 0])
        (let ([nextval (read source)])
          (cond
            ; Are we at the end of the file?
            ; Then stop and return our running sum.
            [(eof-object? nextval) 
             (close-input-port source) 
             sum-so-far]
            ; Have we just read a number?
            ; If so, add it to the sum of the remaining numbers.
            [(number? nextval) 
             (kernel (+ nextval sum-so-far))]
            ; Hmmm ... not a number.  Skip it.
            [else 
             (kernel sum-so-far)]))))))
```

Clean up a bit

