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!
@a.nastaev Take a look at the list of read
functions here:
Look in particular at read-lines
and read-words
and read-words/line
.
@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?
Thanks again for helping me out!
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.
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!
The alternative to using read-words
and friends is to use read-file
and then split the string up into words afterwards.
I think it is easier to use read-words/line.
What does (read-words/line "letter.dat")
return?
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" '())))))))))
Oh! I misunderstood the task.
I think you have solved task correctly.
Open your output file “letter.dat” in say Notepad (window) or TextEdit (mac).
Oh, really?! Well, it is a good news! Thanks a lot!
Should look fine.
Yes! Thanks a lot!!!
The \n is simply the way newlines are printed inside string literals.
You can try:
(display (read-file (main "in-fst.dat" "in-lst.dat" "in-signature.dat" "letter.dat")) )
to see you letter displayed inside DrRacket.
Do I need to define somehow display
? I’m getting an error display: this function is not defined
Hmm. Turns out display
isn’t available until the “Advanced” language.
You can temporarily switch to it and see how it works.
Great! Thanks a lot!