a.nastaev
2019-3-11 14:01:57

Hello guys, I’m asking help again with exercise 31: https://htdp.org/2018-01-06/Book/part_one.html#%28counter._%28exercise._letter-writing%29%29 And here is my solution: (require 2htdp/batch-io) ;letter function----------------------- (define (letter fst lst signature-name) (string-append (opening fst) "\n\n" (body fst lst) "\n\n" (closing signature-name))) ;first name function----------------- (define (opening fst) (string-append "Dear " fst ",")) ;letter body - first and last name function----------------- (define (body fst lst) (string-append "We have discovered that all people with the" "\n" "last name " lst " have won our lottery. So, " "\n" fst ", " "hurry and pick up your prize.")) ;signature function--------------- (define (closing signature-name) (string-append "Sincerely," "\n\n" signature-name "\n")) ;letter-writing batch program--------------------------- (define (main in-fst in-lst in-signature out) (write-file out (letter (read-file in-fst) (read-file in-lst) (read-file in-signature)))) ;(write-file "in-fst.dat" "John") ;(write-file "in-lst.dat" "Smith") ;(write-file "in-signature.dat" "Matt") (read-file (main "in-fst.dat" "in-lst.dat" "in-signature.dat" "letter.dat"))

My letter.dat file has properly formatted and with a given names text. But when I’m trying to readi it by read-file the output is obviously is just unformatted string: "Dear John,\n\nWe have discovered that all people with the\nlast name Smith have won our lottery. So, \nJohn, hurry and pick up your prize.\n\nSincerely,\n\nMatt"

So, how can I read the file in a properly formatted way? What am I missing here?

Thank a lot in advance!


soegaard2
2019-3-11 14:11:58

@a.nastaev Take a look at the list of read functions here:



soegaard2
2019-3-11 14:12:14

Look in particular at read-lines and read-words and read-words/line.


a.nastaev
2019-3-11 14:37:53

@soegaard2 thanks as usual for help! Тhat is my dilemma. Do I need to figure out those functions you’ve mentioned just myself or I have to rely only on the material provided in the book?! What I’m trying to say: read-file reads what is inside the file and produces it as a string. Obviously, I need a find a way to produce it in a way it looks in the file. I can google it or just read docs (as you’ve suggested), but nothing about that was said in the book. And that is my confusion. I don’t mind to be proactive and made extra work, but is it the purpose of the book?


a.nastaev
2019-3-11 14:38:43

Thanks again for helping me out!


soegaard2
2019-3-11 14:39:53

Well, most often the exercises follows the book. However I haven’t taught after the new book (some years ago I taught CS using the first edition), so I can’t remember what the text says exactly.


a.nastaev
2019-3-11 14:44:11

No problem! And obviously you don’t have to investigate! It is too much! Thanks again! it is clear now! And again, I really appreciate your help and time!


soegaard2
2019-3-11 14:45:33

The alternative to using read-words and friends is to use read-file and then split the string up into words afterwards.


soegaard2
2019-3-11 14:45:52

I think it is easier to use read-words/line.


soegaard2
2019-3-11 14:46:18

What does (read-words/line "letter.dat") return?


a.nastaev
2019-3-11 14:48:09

it is: (cons "Dear John," (cons "" (cons "We have discovered that all people with the" (cons "last name Smith have won our lottery. So, " (cons "John, hurry and pick up your prize." (cons "" (cons "Sincerely," (cons "" (cons "Matt" '())))))))))


soegaard2
2019-3-11 14:49:20

Oh! I misunderstood the task.


soegaard2
2019-3-11 14:50:11

I think you have solved task correctly.


soegaard2
2019-3-11 14:50:44

Open your output file “letter.dat” in say Notepad (window) or TextEdit (mac).


a.nastaev
2019-3-11 14:50:47

Oh, really?! Well, it is a good news! Thanks a lot!


soegaard2
2019-3-11 14:50:49

Should look fine.


a.nastaev
2019-3-11 14:51:16

Yes! Thanks a lot!!!


soegaard2
2019-3-11 14:51:53

The \n is simply the way newlines are printed inside string literals.


soegaard2
2019-3-11 14:52:09

You can try:


soegaard2
2019-3-11 14:52:16

(display (read-file (main "in-fst.dat" "in-lst.dat" "in-signature.dat" "letter.dat")) )


soegaard2
2019-3-11 14:52:26

to see you letter displayed inside DrRacket.


a.nastaev
2019-3-11 14:56:27

Do I need to define somehow display? I’m getting an error display: this function is not defined


soegaard2
2019-3-11 15:12:00

Hmm. Turns out display isn’t available until the “Advanced” language.


soegaard2
2019-3-11 15:12:38

You can temporarily switch to it and see how it works.


a.nastaev
2019-3-11 15:30:34

Great! Thanks a lot!