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

_Overview_

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

### News / Etc.

* Sorry about the Website weirdness on Friday.  The style sheet that
  I rely on changed significantly.
* 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 "Write 301 exam and write 151 project", so no grading
      will get done.
* <http://pioneers.grinnell.edu/news/2017/11/3/womens-volleyball-kuhn-named-mwc-volleyball-newcomer-of-the-year.aspx>
* It's preregistration week.  I encourage you to consider CSC 161.
  (Weinman and Walker are both responsible graders.)

### Upcoming Work

* [Writeup for class 28](../writeups/writeup28) due Monday at 10:30 p.m.
    * Exercise 5.  (No documentation necessary, but appreciated.)
    * To: <csc151-01-grader@grinnell.edu>
    * Subject: CSC 151.01 Writeup 28 (YOUR NAMES)
* [Writeup for class 29](../writeups/writeup29) due Wednesday at 10:30 p.m.
    * Exercise 6.  No documention necessary.
    * To: <csc151-01-grader@grinnell.edu>
    * Subject: CSC 151.01 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)

### Extra credit (Misc)

* Pioneer Weekend, this coming weekend.  Design a business idea (can be
  a nonprofit / for social good) have fun, get advice, and make money.
    * Sign up by Nov. 8.

### Other good things

### Questions

Review of higher-order programming
----------------------------------

What did we learn on Friday?  

* Ways of combining procedures.
* `l-s`, that procedure that E hates.  
    * `(l-s proc val)` == `(section proc val <>)`
    * Note: We can write `l-s`
* We learned `apply`, which is kind of like `map` and kind of like
  `reduce`, but different.
* We can write procedures that take another procedure as a parameter.
    * Procedure parameters look like normal parameters.
    * We just use them in the "context" of procedures.  If f is a parameter,
      we can write (f 1) or (map f (iota 5) or ....
* We can write procedures that return another proceudre as a result.
    * Most typically, use a second lambda to define/return the new procedure.

Lab
---

Why am I able to read a file in your account?
  : I set permissions that way.  (Linux lets you set permissions on files in your account so that others can read them.  There are a variety of options.  You'll learn how to do so in CSC 161.)

How do I "create a file in the interactions pane"?
  : `(define outport (open-output-file "/home/username/Desktop/myinfo"))`

How do I write to that file?
  : `(write stuff outport)`
  : `(display stuff outport)`
  : `(newline outport)`

What should I do when I'm done?
  : `(close-output-port outport)`

How should I overwrite an existing file
  : `(define outport (open-output-file "/home/username/Desktop/myinfo"
                                       #:exists 'replace))`

<<<<<<< Updated upstream
Writeup: Exercise 6.  Reload the lab before doing it.
=======
Writeup: Exercise 6
>>>>>>> Stashed changes

Debrief
-------

How did you figure out that the sum was correct?
  : `(apply + (map string->number (file->words FILE)))`
  : Open the file in DrRacket.  Put `(+` at the start.  Put ')' at the end.
    Evaluate.
  : Open the file in a spreadsheet and use the spreadsheet sum.

An incomplete definition of `file-size`.

```
(define file size
  (lambda (file-name)
    (let ([source (open-input-file file-name)])
      (let kernel ([size-so-far 0])
        (let ([nextval (read-char source)])
          (cond
            [(eof-object? nextval) 
             (close-input-port source) 
             size-so-far]
            [(char? nextval) 
             (kernel (+ nextval size-so-far))]
            [else 
             (kernel size-so-far)]))))))
```

Problem: `nextval` can be the eof object or a character, and nothing else.
So ...

```
(define file size
  (lambda (file-name)
    (let ([source (open-input-file file-name)])
      (let kernel ([size-so-far 0])
        (let ([nextval (read-char source)])
          (cond
            [(eof-object? nextval) 
             (close-input-port source) 
             size-so-far]
            [(char? nextval) 
             (kernel (+ nextval size-so-far))]
            [else 
             (kernel size-so-far)]))))))
```

Note: By default, you can only read from existing files and write to 
new files.

```
(define do-with-all
  (lambda (proc lst)
    (when (not (null? lst))
      (proc (car lst))
      (do-with-all proc (cdr lst)))))
```
