我有一个这样的列表:
'(("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))
Run Code Online (Sandbox Code Playgroud)
如何计算列表中每个元素的汽车中术语的总出现次数?基本上我想要:
(("Alpha" . 4)
("Beta" . 5)
("Gamma" . 3)
("Rho" . 1))
Run Code Online (Sandbox Code Playgroud)
不,这不是功课.我还没有"在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 …Run Code Online (Sandbox Code Playgroud) 我想删除字符串末尾的一些字符.
我做了这个功能:
(defun del-delimiter-at-end (string)
(cond
((eq (delimiterp (char string (- (length string) 1))) nil)
string )
(t
(del-delimiterp-at-end (subseq string 0 (- (length string) 1))) ) ) )
Run Code Online (Sandbox Code Playgroud)
有了这个 :
(defun delimiterp (c) (position c " ,.;!?/"))
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么它不起作用.我有以下错误:
Index must be positive and not -1
Run Code Online (Sandbox Code Playgroud)
请注意,我想在字符串列表中拆分字符串,我已经在这里看了一下:
但是如果字符串的结尾是分隔符,它就不起作用,这就是我试图这样做的原因.
我究竟做错了什么?提前致谢.