Xah Talk Show 2025-07-16 Ep682 Ask Grok AI to Generate Hashtable in Emacs Lisp

xah talk show ep682 2a51c
xah talk show ep682 2a51c

timestamp

Ask Grok AI Generate Code of Hashtable in Emacs Lisp

https://x.com/i/grok/share/UXOzKGzitW6KeP16oMxVYlGdI

my evaluation: perfect.

Grok AI Code Emacs Lisp Hashtable 2025-07-16 2eb15
Grok AI Code Emacs Lisp Hashtable 2025-07-16 2eb15
;; Create a hash table
(defun create-hash-table (&optional size)
  "Create a new hash table with optional SIZE."
  (make-hash-table :test 'equal :size (or size 100)))

;; Put key-value pair into hash table
(defun hash-put (table key value)
  "Store KEY with VALUE in TABLE."
  (puthash key value table))

;; Get value from hash table
(defun hash-get (table key &optional default)
  "Retrieve value for KEY from TABLE, return DEFAULT if not found."
  (gethash key table default))

;; Remove key-value pair from hash table
(defun hash-remove (table key)
  "Remove KEY and its value from TABLE."
  (remhash key table))

;; Check if key exists in hash table
(defun hash-contains (table key)
  "Return t if KEY exists in TABLE, nil otherwise."
  (not (eq (gethash key table 'not-found) 'not-found)))

;; Get all keys in hash table
(defun hash-keys (table)
  "Return a list of all keys in TABLE."
  (let (keys)
    (maphash (lambda (key _) (push key keys)) table)
    keys))

;; Get all values in hash table
(defun hash-values (table)
  "Return a list of all values in TABLE."
  (let (values)
    (maphash (lambda (_ value) (push value values)) table)
    values))

;; Get hash table size
(defun hash-size (table)
  "Return the number of entries in TABLE."
  (hash-table-count table))

;; Clear hash table
(defun hash-clear (table)
  "Remove all entries from TABLE."
  (clrhash table))

;; Example usage:
(defun hash-table-example ()
  "Demonstrate hash table usage."
  (let ((my-table (create-hash-table)))
    ;; Add some key-value pairs
    (hash-put my-table "name" "John")
    (hash-put my-table "age" 30)
    (hash-put my-table "city" "New York")

    ;; Print some values
    (message "Name: %s" (hash-get my-table "name"))
    (message "Age: %s" (hash-get my-table "age"))
    (message "Has city? %s" (hash-contains my-table "city"))
    (message "Keys: %s" (hash-keys my-table))
    (message "Values: %s" (hash-values my-table))
    (message "Size: %d" (hash-size my-table))

    ;; Remove an entry
    (hash-remove my-table "age")
    (message "After removing age, size: %d" (hash-size my-table))

    ;; Clear the table
    (hash-clear my-table)
    (message "After clearing, size: %d" (hash-size my-table))))

(hash-table-example)

Meaning of Symbols in Emacs Lisp

xx = 3
print(xx)
# 3
(setq xx 3)
;; 3

(quote xx)
;; xx

Download Wolfram Language

download Wolfram language 2025-07-16 2c019
download Wolfram language 2025-07-16 2c019

Emacs: Xah Fly Keys. Mode Status Indicator