LISP Cannot take CAR of T -


i trying evaluate each atom of list , see if it's equal number provided , remove if not running slight problem.

i wrote following code:

(defun equal1(v l)      (cond((= (length l) 0))           (t (cond( (not(= v (car(equal1 v (cdr l))))) (cdr l) )))      ) )  (equal1 5 '(1 2 3 4 5)) 

i obtain following error

error: cannot take car of t. 

if add (write "hello") action if true, following error obtained:

error: cannot take car of "hello". 

i'm still quite new lisp , wondering going on , how fix evaluate each atom , remove if not, cdr l action.

there couple of things improve function.

firstly, let's indent

(defun equal1 (v l)   (cond     ((= (length l) 0))     (t (cond          ((not (= v (car (equal1 v (cdr l))))) (cdr l)))))) 

rather saying (= (length l) 0), can use (zerop (length l)). minor sylistic point. worse branch returns no value. if list l empty should return?

the issue function in t branch of first cond.

what want is

  1. remove list item same value v
  2. keep item not = v

the function should return list.

the expression

(cond    ((not (= v (car (equal1 v (cdr l))))) (cdr l))) 

is trying (i think) deal both conditions 1 , 2. it's not working.

we have recall items in list , result of equal function needs list. in expression above result of function boolean , hence result of function call boolean.

the function needs step along each element of list , when sees matching value, skip it, otherwise use cons function build filtered output list.

here skeleton out. notice don't need embedded cond , have 3 conditions deal - list empty, filter value out, or continue build list.

(defun equal-2 (v l)   (cond     ((zerop (length l))  nil)     ((= v (car l))  <something goes here>)      ;skip or filter value     (t (cons (car l) <something goes here>)))) ;build output list 

of course, being common lisp, there built-in function this. can remove-if...


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -