emacs - 计算项目的优雅方式

标签 emacs lisp elisp

我有一个这样的列表:

  '(("Alpha" .  1538)
    ("Beta"  .  8036)
    ("Gamma" .  8990)
    ("Beta"  .  10052)
    ("Alpha" .  12837)
    ("Beta"  .  13634)
    ("Beta"  .  14977)
    ("Beta"  .  15719)
    ("Alpha" .  17075)
    ("Rho"   .  18949)
    ("Gamma" .  21118)
    ("Gamma" .  26923)
    ("Alpha" .  31609))

如何计算列表中每个元素的术语在汽车中出现的总次数?基本上我想要:

(("Alpha" . 4)
 ("Beta" . 5)
 ("Gamma" . 3)
 ("Rho" . 1))

不,这不是家庭作业。我只是还没有“用 Lisp 思考”的东西。

在 C# 中,我会使用 LINQ 来执行此操作。我也可以用 lisp 来做,使用 while 循环等等,但我想做的方式似乎过于复杂。


编辑

这是我的:

(defun count-uniq (list)
  "Returns an alist, each item is a cons cell where the car is
a unique element of LIST, and the cdr is the number of occurrences of that
unique element in the list. "
  (flet ((helper (list new)
                 (if (null list)
                     new
                   (let ((elt (assoc (car list) new)))
                     (helper (cdr list)
                             (if elt
                                 (progn (incf (cdr elt)) new)
                               (cons (cons (car list) 1) new)))))))
    (nreverse (helper list nil))))

最佳答案

(defun freqs (list &optional test key)
  (let ((h (make-hash-table :test test)))
    (dolist (x list)
      (let ((key (if key (funcall key x) x)))
        (puthash key (1+ (gethash key h 0)) h)))
    (let ((r nil))
      (maphash #'(lambda (k v) (push (cons k v) r)) h)
      (sort r #'(lambda (x y) (< (cdr x) (cdr y)))))))

(freqs '(("Alpha" .  1538)
         ("Beta"  .  8036)
         ("Gamma" .  8990)
         ("Beta"  .  10052)
         ("Alpha" .  12837)
         ("Beta"  .  13634)
         ("Beta"  .  14977)
         ("Beta"  .  15719)
         ("Alpha" .  17075)
         ("Rho"   .  18949)
         ("Gamma" .  21118)
         ("Gamma" .  26923)
         ("Alpha" .  31609))
       #'equal #'car)

关于emacs - 计算项目的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050033/

相关文章:

lisp - 禁用 lispworks capi :push-button-panel 上的单个按钮

lisp - 如何知道函数是否评估其参数?

Emacs eshell - 退出时杀死窗口

emacs - 在模板选择之前中止组织捕获时的钩子(Hook)或建议?

java - 在 clojure 中, (= 'a ' a) 指的是 'same atom' 吗?

emacs - 主模式 Hook 配置会影响其他缓冲区

emacs - 在 emacs 中的最后两个缓冲区之间来回切换

security - 扩展 Emacs 编辑加密文本文件

emacs - 是否有更改 emacs 配色方案的指南?

emacs - Scheme 和 Elisp 函数求值的区别