lisp - lisp代码的解释

标签 lisp common-lisp

我开始学习 Lisp,我在书上找到了一段代码作为例子,但我不明白它是做什么用的。你能帮我理解吗?我不知道这样做是否合适。谢谢大家

(defun compress (l1)               
  (cond ((null (cdr l1)) '())
        (t (accumula (car l1) 1 (cdr l1)))))


(defun accumula (val acc lst)
  (cond ((null lst) (cons (comp-list val acc) nil))
        ((eq val (car lst)) (accumula val (1+ acc) (cdr lst)))
        (t (cons (comp-list val acc) (accumula (car lst) 1 (cdr lst))))))

(defun comp-list (val acc)
  (if (> acc 1) (list acc val) val))

最佳答案

这是Run Length Encoding的压缩函数多样性。

(compress '(3 3 4 3 3 2 1 1 1 1 0)) 

会产生

((2 3) 4 (2 3) 2 (4 1) 0)

其中每个子列表中的第一个数字是第二个数字在原始序列中重复的次数。

从示例中看起来不太像,但对于数字重复很多的长序列,您可以显着节省存储成本。

关于lisp - lisp代码的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467997/

相关文章:

list - 检查对角线时出现逻辑错误 - nQueens

oop - 对这个 "oop under-the-hood"计数器示例如何工作感到非常困惑

recursion - Common-Lisp 中的递归阶乘函数

arrays - Lisp 格式过程应用于数组

common-lisp - 普通口齿不清 : how to display a dot?

lisp - 对多个原子使用读取时条件化

lambda - 用 lisp 代码编写的 Lambda 微积分无穷大骑士

unit-testing - Common Lisp 是否有模拟/ stub 框架?

lisp - 在普通的 lisp 中,如何以可移植的方式检查对象的类型

python - 相当于 python 习语的常见 lisp