Re: [racket-users] "Unbound Identifier" (Lists)
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 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 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 >> wrote: >> >>> >>> Hi, >>> >>> Presumably you are using 'head' to get the first element of a list. >>> However, there is no function 'head' for lists. >>> s
Re: [racket-users] "Unbound Identifier" (Lists)
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 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 racket-users+unsubscr...@googlegroups.com. > 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>
[racket-users] "Unbound Identifier" (Lists)
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 racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/ae6488a3-3b70-4de7-8a1f-8f5526e63580n%40googlegroups.com.
[racket-users] Questions Regarding My First Program
Hi everyone, I am new here, and I have a couple of questions related to the code that follows this message. 1. I want to learn functional programming. When it comes to solving the problem of making a simple chatbot such as this, is there an approach that is more consistent with functional programming? 2. I would prefer to not have to use multiple spaces every time I want to indent. Does Dr. Racket have anything comparable to the tab functionality of other programs? Thank you for letting me join your community! Karona ;example.rkt #lang racket (require racket/match) (define (say str) (printf "Chatbot: ~a\n" str)) (define (handle-input str) (cond [(regexp-match #px"(?i:Hello,* world!)" str) (say "I would not know about the rest of the world, but I can hear \ you just fine.")] [(regexp-match #px"(?i:I( am|'m) learning how to program in Racket,* world!)" str) (say "Racket is a great language, and it is lovely that you are \ learning it, but does literally everyone need to know?")] [(regexp-match #px".*,+\\s*world!" str) (say "Did the whole world really need to hear that?")] [else (say "Did you really just say something without addressing the \ world? I am so proud of you! :,)")])) (let loop () (display "Input: ") (define str (read-line (current-input-port) 'any)) (handle-input str) (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 racket-users+unsubscr...@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.