Search code examples
lispcommon-lispclisp

Counting words which aren't starting with given letter


So I wrote this function in Lisp which counts how many words aren't starting with a given letter in a list. However I now need to edit it and not use "let" in my function (but keep "char" and "string"). Feeling a bit blocked as I've started Lisp not so long go ! Anyone would be able to help me ?

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

Thats what I did but need to remove the "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)))) ) ) )

Solution

  • One solution is to use an &aux variable in the argument list. This lets you bind variables in the function much like let does. This is pretty much a way to use let at the start of the function without explicitly using 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))))))