;======== ; Fall 2024 - CS 111 ; Week 6 Lab Exercise ; ; date: 2024-10-04 ;======== ;************* ; *** NOTE *** ; This lab has even MORE explanations than ; usual! Look for ;*** DO THIS *** ; ...right before the parts YOU are to add. (require 2htdp/image) (require 2htdp/universe) ;======== ; USING pair-programming: ; 1. COPY and PASTE the contents of this ; file into a DrRacket Definitions window ; 2. ADD comments and expressions *to* this ; file as specified (one student saying ; what to type, the other student typing it ; into DrRacket) ; 3. RUN the resulting file frequently ; along the way, fixing any errors that ; arise, saving the Definitions window ; frequently, say as lab6.rkt ; 4. When you are done, use Gmail to MAIL a ; copy of the resulting file, ; ; ***AS WELL AS YOUR FILE practice.txt***, ; ; to BOTH of you ; 5. And, EACH of you should SUBMIT this file, ; ; ***AND ALSO YOUR FILE practice.txt***, ; ; on Canvas ;======== ;*** DO THIS *** ;===== ; Leave a blank line, and then put COMMENT(s) ; containing BOTH of your names: ;======== ; PROBLEM 1 - trying out some file input/output ;======== ; this lets you use DrRacket's file input/output ; functions, especially useful for ; "batch"-style programs (require 2htdp/batch-io) ;----- ; 1 part a ;----- ; NOTE: you can say that you want a ; newline character in a Racket string by ; writing it as "\n" ;----- ; Consider the following signature and ; purpose comment for function write-file, ; from 2htdp/batch-io: ;======== ; signature: write-file: string string -> string ; purpose: expects the name of a file ; written as a string ; AND the string to write to that file, ; ; has the SIDE-EFFECT of trying to ; write that string to that ; file (creating that file if necessary), ; ; and returns the name of the file "should see the name of the file written:" "----------------------------------------" ;*** DO THIS *** ;----- ; WRITE an expression, using write-file, whose ; side-effects should be: ; * to create a file named practice.txt ; ; * that contains AT LEAST FIVE lines ; worth of contents of your choice, ; ; * making sure that AT LEAST TWO lines ; contain MORE THAN ONE WORD each. ; ;(Remember: write-file only expects TWO ; string arguments -- ; the name of the file to write to, ; and the string to write there -- ; BUT you can use "\n" within a string ; to say that you want a ; newline character.) ;----- ; 1 part b ;----- ; Consider the following signature and ; purpose comment for function read-file, ; from 2htdp/batch-io: ;======== ; signature: read-file: string -> string ; purpose: expects the name of a file ; written as a string, ; ; has the SIDE-EFFECT of trying to ; open that file and read its contents, ; ; then returns the contents read as ; a single string "" "practice.txt contents as a SINGLE string:" "----------------------------------------" ;*** DO THIS *** ;----- ; WRITE an expression, using read-file, ; whose value will be a single string ; containing all of the contents ; of practice.txt: ;----- ; 1 part c ;----- ; Consider the following signature and ; purpose comment for function read-lines, ; from 2htdp/batch-io: ;======== ; signature: read-lines: string -> list ; purpose: expects the name of a file ; written as a string, ; ; has the SIDE-EFFECT of trying to ; open and read the contents of that file, ; ; and returns the read contents as a ; LIST of strings, each line being one ; string in the list "" "practice.txt contents as a list of " " line-strings:" "----------------------------------------" ;*** DO THIS *** ;----- ; WRITE an expression, using read-lines, ; whose value will be a list of strings, ; such that each string is one ; line from practice.txt: ;----- ; 1 part d ;----- ; Consider the following signature and ; purpose comment for function read-words, ; from 2htdp/batch-io: ;======== ; signature: read-words: string -> list ; purpose: expects the name of a file ; written as a string, ; ; has the SIDE-EFFECT of trying to ; open and read the contents of that file, ; ; and returns those contents as a list ; of strings, with each ; white-space-separated "chunk" being a ; string in this list "" "practice.txt contents as a list of" " word-strings:" "----------------------------------------" ;*** DO THIS *** ;----- ; WRITE an expression, using read-words, ; whose value will again be a list of ; strings -- but THIS time, each string ; will be one white-space-separated ; "word"/chunk from practice.txt: ;----- ; 1 part e ;----- ; Consider the following signature and ; purpose comment for function ; read-words/line, from 2htdp/batch-io: ;======== ; signature: read-words/line: string -> list ; purpose: expects the name of a file ; written as a string, ; ; has the SIDE-EFFECT of trying to open ; and read the contents of that file, ; ; and returns its contents as a list of ; lists, one sub-list per line, with ; the white-space-separated ; "words"/chunks in that line as ; strings in that line's list "" "practice.txt contents as a list of " " line-lists of word-strings:" "----------------------------------------" ;*** DO THIS *** ;----- ; WRITE an expression, using read-words/line, ; whose value will be a list of lists of ; strings -- such that each sub-list is ; a list of white-space-separated ; "word"/chunk strings from one line ; from practice.txt: ;======== ; PROBLEM 2 - writing a function that ; "walks through" a list ;======== ;==== TEMPLATE for a function that needs ; to "walk through" all of the elements of a ; variable-length list ; ; (define (my-list-funct my-list ...) ; (cond ; [(empty? my-list) ...] ; [else ; (... (first my-list) ; (my-list-funct (rest my-list) ...))] ; ) ; ) ;*** DO THIS *** ;-------- ; Using the DESIGN RECIPE, design a function ; sum-list, which expects a list of numbers, ; and just returns the SUM of the numbers in ; that list. ; * That is, if this function has as ; its argument a list of numbers ; containing 7, 10, and 41, then it ; would return 58. ; * (And, if called with the empty list ; as its argument, then it should ; return 0.) ;===== ; Remember: once you have Run these and are ; satisfied with them, ; * Use Gmail to EMAIL copies of this file ; ***AND ALSO YOUR FILE practice.txt***, ; to BOTH of you ; ; * BOTH of you should submit this file, ; ***AND ALSO YOUR FILE practice.txt***, ; on Canvas ;=====