Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |27.2|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;Exercise 27.2.1. Determine what the list-of-lines representation for empty, (list 'NL), and (list 'NL 'NL) should be. Why are these examples important test cases?
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;A file is either
7 12687dd9 2023-08-04 jrmu ;1. empty or
8 12687dd9 2023-08-04 jrmu ;2. (cons s f)
9 12687dd9 2023-08-04 jrmu ;where s is a symbol and f is a file.
10 12687dd9 2023-08-04 jrmu
11 12687dd9 2023-08-04 jrmu ;Examples
12 12687dd9 2023-08-04 jrmu ;
13 12687dd9 2023-08-04 jrmu ;(file->list-of-lines empty)
14 12687dd9 2023-08-04 jrmu ;empty
15 12687dd9 2023-08-04 jrmu ;(file->list-of-lines (list 'NL))
16 12687dd9 2023-08-04 jrmu ;(list empty)
17 12687dd9 2023-08-04 jrmu ;(file->list-of-lines (list 'NL 'NL))
18 12687dd9 2023-08-04 jrmu ;(list empty empty)
19 12687dd9 2023-08-04 jrmu
20 12687dd9 2023-08-04 jrmu ;The NEWLINE character is represented by 'NL
21 12687dd9 2023-08-04 jrmu (define NEWLINE 'NL)
22 12687dd9 2023-08-04 jrmu
23 12687dd9 2023-08-04 jrmu ;file->list-of-lines : file -> (listof (listof symbols))
24 12687dd9 2023-08-04 jrmu ;Converts a-file into a (listof (listof symbols)) representing the file. Each new list starts right at the end of a newline symbol and ends at the first occurrence of a newline symbol. file->list-of-lines uses generative recursion: the first line is appended to the front of the remainder of the file that has already been converted into a list of lines.
25 12687dd9 2023-08-04 jrmu ;Termination Argument: Since each application of file->list-of-lines necessarily shortens the argument for the new generative recursion, the paramater must get smaller over time and eventually become the empty list, for which a trivial solution is available.
26 12687dd9 2023-08-04 jrmu
27 12687dd9 2023-08-04 jrmu (define (file->list-of-lines a-file)
28 12687dd9 2023-08-04 jrmu (cond
29 12687dd9 2023-08-04 jrmu [(empty? a-file) empty]
30 12687dd9 2023-08-04 jrmu [else (cons (first-line a-file)
31 12687dd9 2023-08-04 jrmu (file->list-of-lines (remove-first-line a-file)))]))
32 12687dd9 2023-08-04 jrmu
33 12687dd9 2023-08-04 jrmu ;first-line : file -> (listof symbols)
34 12687dd9 2023-08-04 jrmu ;Returns the first line of a-file.
35 12687dd9 2023-08-04 jrmu
36 12687dd9 2023-08-04 jrmu (define (first-line a-file)
37 12687dd9 2023-08-04 jrmu (cond
38 12687dd9 2023-08-04 jrmu [(symbol=? NEWLINE (first a-file)) empty]
39 12687dd9 2023-08-04 jrmu [else (cons (first a-file)
40 12687dd9 2023-08-04 jrmu (first-line (rest a-file)))]))
41 12687dd9 2023-08-04 jrmu
42 12687dd9 2023-08-04 jrmu ;remove-first-line : file -> file
43 12687dd9 2023-08-04 jrmu ;Given a-file, remove the first line.
44 12687dd9 2023-08-04 jrmu
45 12687dd9 2023-08-04 jrmu (define (remove-first-line a-file)
46 12687dd9 2023-08-04 jrmu (cond
47 12687dd9 2023-08-04 jrmu [(symbol=? NEWLINE (first a-file)) (rest a-file)]
48 12687dd9 2023-08-04 jrmu [else (remove-first-line (rest a-file))]))
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu (file->list-of-lines '(Hi my name is Joe NL
51 12687dd9 2023-08-04 jrmu and I live in a button factory NL
52 12687dd9 2023-08-04 jrmu I have a wife NL
53 12687dd9 2023-08-04 jrmu and three kids NL))