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.

Reply via email to