[COLUG] Lisp variable scope in function
Steven Huwig
shuwig at columbus.rr.com
Thu Nov 30 22:16:42 EST 2006
On Nov 30, 2006, at 4:51 PM, David Riggs wrote:
> Can anyone help me with my emacs lisp confusion?
>
> (defun mytest () (interactive)
> (let ((dot '("a" . 0)) (n 0)) ;;set both to zero
> (setcdr dot (1+ (cdr dot))) ;; incr both by one
> (setq n (1+ n))
> (print dot)(print n)))
I think your trouble is using '("a" . 0) instead of (cons "a" 0).
Does this work?
(defun mytest () (interactive)
(let ((dot (cons "a" 0)) (n 0)) ;;set both to zero
(setcdr dot (1+ (cdr dot))) ;; incr both by one
(setq n (1+ n))
(print dot)(print n)))
This issue -- modifying a quoted literal list -- is mentioned
at the top of http://wiki.alu.org/Lisp_Gotchas .
> I have a little function to search through some data and count up
> occurances of found items, which I keep track of with an a-list.
Would this be something like:
(defun count-items (items)
(let ((counts (make-hash-table)))
(dolist (item items)
(puthash item (1+ (gethash item counts 0)) counts))
(let ((results '()))
(maphash (lambda (k v) (push (cons k v) results)) counts)
results)))
(count-items '(a b a a b c d b d))
((d . 2) (c . 1) (b . 3) (a . 3))
-- Steve
More information about the colug432
mailing list