计算不以给定字母开头的单词

Counting words which aren't starting with given letter

所以我用 Lisp 编写了这个函数,它计算列表中有多少单词不是以给定字母开头的。 但是我现在需要编辑它而不是在我的函数中使用“let”(但保留“char”和“string”)。 感觉有点受阻,因为我刚开始使用 Lisp 没多久!任何人都可以帮助我吗?

示例: (others 'n '(art nose foot nose take silence never)) => 4

这就是我所做的,但需要删除“let”:

(defun others (x liste)
  (let ((c (char (string x) 0)))
    (cond
      ((not liste) 0)
      ((char= (char (string (car liste)) 0) c) (others x (cdr liste)))
      (t (+ 1 (others x (cdr liste)))) ) ) )

一种解决方案是在参数列表中使用 &aux 变量。这使您可以像 let 一样在函数中绑定变量。这几乎是一种在函数开头使用 let 而无需显式使用 let.

的方法
(defun others (x liste &aux (c (char (string x) 0)))
   (cond
      ((not liste) 0)
      ((char= (char (string (car liste)) 0) c) (others x (cdr liste)))
      (t (+ 1 (others x (cdr liste))))))

这是一个迭代版本:

(loop for word in '("art" "nose" "foot" "nose" "take" "silence" "never")
      for c = #\n
      count (not (char= c (char word 0))))

(循环可以通过https://lispcookbook.github.io/cl-cookbook/iteration.html的例子学习)

另一种内置方式是这样的:

> (count 'n
         '(art nose foot nose take silence never)
         :test #'(lambda (a b)
                   (string/= a b :end1 1 :end2 1)))
-> 4