I can't speak to DrRacket since I never use it, so I'll focus more on the first question.
Functional programming typically deals with these considerations
(among others):
A goal to make the program produce the same output given the same
input, every time.
One example would be to redefine `say` such that it doesn't
immediately print. Maybe just return the string you want said.
(define (say str)
(format "Chatbot: ~a\n" str))
(I'd personally use a struct representing a message, sender,
etc. but that's beside the point)
This change makes the function "pure" in that the same input
returns the same output, and there are no side-effects in doing
so. But this creates a problem where you need to "rope in" that
value somewhere. How that happens depends on the program. A
program that reports download progress will handles side-effects
differently than a program that prints a report before stopping.
One approach is to update program state in the same (functional)
way. If we assume the chat history is a list, where the first
element is the most recent message, then this version of `say`
adds a string to the chat.
(define (say chat-history str)
(cons (format "Chatbot: ~a\n" str)
chat-history))
I'd start with this kind of thinking until you get to the point where side-effects are unavoidable. Since your program appears interactive, you can still print in the loop. But think about what your program would be like if you print ONLY in that loop.
Others can probably say more, but hopefully this helps you start.
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/2f107ab5-2efd-4c33-83f6-eb5751664cb6n%40googlegroups.com.
-- ~slg