0

i am new to lisp and in my code i want to convert 3 first bit to integer and check if number of ones in next bits is equal to that or not this is my code:

 (defun new (vi n res)(
(cond ( (= vi nil) ((cond ((= 0 res) t) )))
 (    (= 2 n) (new (cdr vi) 1 (* (car vi) 4)) ) 
      ((= 1 n) (new (cdr vi) 0 (+ (res) (* (car vi) 2)) ))  
      ((= 0 n) (new (cdr vi) -1 (+ (res) (car vi ))  ))
       ((= vi nil) (nil))
      ((= -1 n) (new (cdr vi) -1 (- res (car vi)) )) )))

but it has this error: (COND ((= VI NIL) ((COND ((= 0 RES) T)))) ((= 2 N) (NEW (CDR VI) 1 (* (CAR VI) 4))) ((= 1 N) (NEW (CDR VI) 0 (+ (RES) (* (CAR VI) 2)))) ((= 0 N) (NEW (CDR VI) -1 (+ (RES) (CAR VI)))) ((= VI NIL) (NIL)) ((= -1 N) (NEW (CDR VI) -1 (- RES (CAR VI))))) should be a lambda expression

1 Answer 1

1

You put extra () around for cond, which makes lisp think it should be callable, remove extra () would fix the issue.

 (defun new (vi n res)
    (cond ((= vi nil) (cond ((= 0 res) t) ))
          ((= 2 n) (new (cdr vi) 1 (* (car vi) 4))) 
          ((= 1 n) (new (cdr vi) 0 (+ (res) (* (car vi) 2))))  
          ((= 0 n) (new (cdr vi) -1 (+ (res) (car vi ))))
          ((= vi nil) (nil))
          ((= -1 n) (new (cdr vi) -1 (- res (car vi))))))
Sign up to request clarification or add additional context in comments.

5 Comments

this problem solved but when I give parameters to it like this :(new ( 0 1 0 0 0 0 1 0 0 1 0) 2 0 ) it errors: - EVAL: 0 is not a function name; try using a symbol instead
@MohammadaliEftekhari: 0 is not a function, so the error is correct.
how can i fix this i dont want 0 to be a function?
@MohammadaliEftekhari there is a function LIST to create a list. There are some basic tutorials about Lisp you might need to check out first.
i read some but this language is a little hard to understand so i wanted to learn with some examples

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.