Your solution is so elegant. Thank you! >> PS Do I have the salutation right? Is it 'Hello, IF' or 'Hello, Karona'? <<
You may call me Karona. Thank you for asking. :) Karona On Wed, Feb 24, 2021 at 11:14 PM David Storrs <[email protected]> wrote: > Hi IF, > > I think this is what you want. You were right about the issue -- you were > consing onto the history list but then immediately throwing away the result > because it wasn't modifying the original history value. > > #lang racket > > (struct message (str sender recipient) #:transparent) > ; This returns a list consisting of the message that we are sending > > ; out; followed by the message that the participant posted. It will > > ; be appended; to the history. > > (define (say m str) > (displayln str) > (list (message str "me" "participant") m)) > > ; start off with an empty history > (let loop ([history '()]) > (display "Input: ") > (define input (message (read-line (current-input-port) 'any) > "participant" "me")) > (display "Chatbot: ") > (loop > (append > (match (message-str input) > [(regexp #rx"(?i:Hello,* world!)") > (say input "I would not know about the rest of the world, but I can > hear \ > you just fine.")] > [(regexp #px"(?i:I( am|'m) learning how to program in Racket,* > world!)") > (say input "Racket is a great language, and it is lovely that you > are \ > learning it, but does literally everyone need to know?")] > [(regexp #px".*,+\\s*world!") > (say input "Did the whole world really need to hear that?")] > ; if your input is DEBUG then it will print out the history but not add to > it > ["DEBUG" (pretty-print history) '()] > ; if you type something unrecognized it will say so > [else (say input "I didn't understand that")]) > history)) > (loop)) > > > PS Do I have the salutation right? Is it 'Hello, IF' or 'Hello, Karona'? > > > > On Wed, Feb 24, 2021 at 6:21 PM IF Karona <[email protected]> wrote: > >> Thank you! >> >> I can get the code that follows to run, but the chatbot never replies. I >> surmise this is because lists are not mutable. >> >> With the background I have the most obvious way forward would be to use >> an array instead of a list, but that does not strike me as the best >> approach to take while trying to learn functional programming. >> >> Is there a better way through this? >> >> ;example.rkt >> >> #lang racket >> >> (require racket/match) >> >> (struct message (str sender recipient)) >> >> (define chat-history (list (message "" "" ""))) >> >> (define (say m) >> (cons m chat-history)) >> >> (define (log m) >> (cons m chat-history)) >> >> (let loop () >> (display "Input: ") >> (define input (message (read-line (current-input-port) 'any) >> "participant" "me")) >> (define str (message-str input)) >> (log input) >> >> (cond >> >> [(regexp-match #px"(?i:Hello,* world!)" str) >> (say (message "I would not know about the rest of the world, but I >> can hear \ >> you just fine." "me" "participant"))] >> >> [(regexp-match #px"(?i:I( am|'m) learning how to program in Racket,* >> world!)" str) >> (say (message "Racket is a great language, and it is lovely that you >> are \ >> learning it, but does literally everyone need to know?" "me" >> "participant"))] >> >> [(regexp-match #px".*,+\\s*world!" str) >> (say (message "Did the whole world really need to hear that?" "me" >> "participant"))] >> >> [else (say (message "Did you really just say something without >> addressing the \ >> world? I am so proud of you! :,)" "me" "participant"))]) >> >> (define head (first chat-history)) >> (define response (message-str head)) >> (printf "Chatbot: ~a\n" response) >> (loop)) >> >> On Wed, Feb 24, 2021 at 3:11 PM George Neuner <[email protected]> >> wrote: >> >>> >>> Hi, >>> >>> Presumably you are using 'head' to get the first element of a list. >>> However, there is no function 'head' for lists. >>> see: https://docs.racket-lang.org/reference/pairs.html >>> >>> Try using 'first' and 'rest' instead of 'head' and 'tail'. >>> >>> George >>> >>> >>> On 2/24/2021 3:55 PM, IF Karona wrote: >>> >>> Hi everyone, >>> >>> After trying to implement some changes Sage suggested (all errors are my >>> own), I am now encountering the following message (courtesy of DrRacket): >>> >>> "head: unbound identifier in: head" >>> >>> Could someone help me find a fix? >>> >>> As before, I welcome suggestions on how to better do this the functional >>> programming way. >>> >>> Karona >>> >>> ;example.rkt >>> >>> #lang racket >>> >>> (require racket/match) >>> >>> (struct message (str sender recipient)) >>> >>> (define (say chat-history m) >>> (cons m >>> chat-history)) >>> >>> (define (log chat-history m) >>> (cons m >>> chat-history)) >>> >>> (let loop () >>> (display "Input: ") >>> (define input (message (read-line (current-input-port) 'any) >>> "participant" "me")) >>> (define str (message-str input)) >>> (log chat-history input) >>> >>> (cond >>> >>> [(regexp-match #px"(?i:Hello,* world!)" str) >>> (say chat-history (message "I would not know about the rest of the >>> world, but I can hear \ >>> you just fine." "me" "participant" "me" "participant"))] >>> >>> [(regexp-match #px"(?i:I( am|'m) learning how to program in Racket,* >>> world!)" str) >>> (say chat-history (message "Racket is a great language, and it is >>> lovely that you are \ >>> learning it, but does literally everyone need to know?" "me" >>> "participant"))] >>> >>> [(regexp-match #px".*,+\\s*world!" str) >>> (say chat-history (message "Did the whole world really need to hear >>> that?" "me" "participant"))] >>> >>> [else (say chat-history (message "Did you really just say something >>> without addressing the \ >>> world? I am so proud of you! :,)" "me" "participant"))]) >>> >>> (define hd (head chat-history)) >>> (define s (message-str hd)) >>> (printf "Chatbot: ~a\n" s) >>> (loop)) -- >>> 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 [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/racket-users/ae6488a3-3b70-4de7-8a1f-8f5526e63580n%40googlegroups.com >>> <https://groups.google.com/d/msgid/racket-users/ae6488a3-3b70-4de7-8a1f-8f5526e63580n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> >>> -- >>> 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 [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/racket-users/1323324c-372f-ff37-efdb-d888d2c223dc%40comcast.net >>> <https://groups.google.com/d/msgid/racket-users/1323324c-372f-ff37-efdb-d888d2c223dc%40comcast.net?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> 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 [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/racket-users/CAL%3D3WYXN_azQ1rVV6viYm42c83vbCYd-HxXKGDXs4L18Z-KLyg%40mail.gmail.com >> <https://groups.google.com/d/msgid/racket-users/CAL%3D3WYXN_azQ1rVV6viYm42c83vbCYd-HxXKGDXs4L18Z-KLyg%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAL%3D3WYWm0ykJKenP-Rcqy9eUt%3DVC%3DHKigq4YZ5kF_YR6q7aFig%40mail.gmail.com.

