在L.LISP中的任何地方都可以找到符号A [英] Occurrence of symbol A found anywhere in L. LISP
本文介绍了在L.LISP中的任何地方都可以找到符号A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的功能
(defun freq (symbol_A List_L)
(cond ((atom (car List_L))
(cond ((eq (car List_L) symbol_A) t (+ 1 (freq symbol_A (cdr List_L))))
(t 0)))
(T (freq symbol_A (cdr List_L))))
)
我得到一个错误变量ATOM没有值.这是我正在使用
I am getting an error variable ATOM has no value. Here is what I am testing with
(freq 'c '((a c) c e)) --> 2
(freq 'f '(((s) o ) d)) --> 0
(freq 'f '(((f) f) f f)) --> 4
不明白我的错误在哪里.
Can not understand where is my error.
我也尝试过:
(defun freq (a L)
(cond
((null L) 0)
((equal a (car L)) (+ 1 (freq a (cdr L))))
(t (freq a (cdr L)))))
推荐答案
知道nil
是原子,您可以简单地递归遍历每个cons单元格的car
和cdr
.命中原子时,如果匹配则添加1,否则将添加0.
Knowing that nil
is an atom, you can simply recurse over the car
and cdr
of each cons cell. When you hit an atom, add 1 if it matches or 0 if it doesn't.
(defun freq (sym tree)
(if (atom tree)
(if (eq sym tree) 1 0)
(+ (freq sym (car tree)) (freq sym (cdr tree)))))
这篇关于在L.LISP中的任何地方都可以找到符号A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文