"Unbound Identifier" (Lists)

83 views
Skip to first unread message

IF Karona

unread,
Feb 24, 2021, 3:55:43 PM2/24/21
to Racket Users
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))

George Neuner

unread,
Feb 24, 2021, 4:11:04 PM2/24/21
to racket...@googlegroups.com

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
  (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...@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.

IF Karona

unread,
Feb 24, 2021, 6:21:31 PM2/24/21
to racket...@googlegroups.com
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))

David Storrs

unread,
Feb 25, 2021, 12:14:27 AM2/25/21
to IF Karona, Racket Users
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'?



IF Karona

unread,
Feb 25, 2021, 12:43:46 PM2/25/21
to racket...@googlegroups.com
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

David Storrs

unread,
Feb 25, 2021, 2:22:31 PM2/25/21
to IF Karona, Racket Users
On Thu, Feb 25, 2021 at 12:43 PM IF Karona <karo...@gmail.com> wrote:
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


Cool, good to know and I'm glad you liked the solution.

Thinking more about it, it would be better to pull the match out in order to have clearer code, and add a way to end the loop. Something like this:

#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))

(define (find-response input history)

  (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?")]
    ["DEBUG" (pretty-print history) '()]
    [else (say input "I didn't understand that")]))


(let loop ([history '()])
  (display "Input: ")
  (define input  (message (read-line (current-input-port) 'any) "participant" "me"))
  (display "Chatbot: ")
  (if (equal? (message-str input) "DONE")
      (begin (displayln "Bye!") (pretty-print history))
      (loop (append (find-response input history) history))))
 
Reply all
Reply to author
Forward
0 new messages