Jump to content
Sign in to follow this  
AM

how to code a NAND gate?

Recommended Posts

hi all

 

i would like to code a NAND gate with more then two input-items (as extension to AND etc...). here is a simple version of the NAND function with two inputs, but i don't know how to exapnd it to n-inputs without putting the the inputs to in a list (like lisp-internal AND / OR)...

 

https://en.wikipedia.org/wiki/NAND_gate

 

i dont't want it:

(nand '(t t t nil))

 but like to have

(nand t nil nil t t t)

 

when i get a solution for that i will code an XOR, NOR etc....

 

 

 

so the "problem" is: how to manage in DEFUN more then two inputs (don't work with &optional, i think) i tried it and failed)...

any ideas, lisp-nerds? 🙂 thanx! andré

 

;;; easy with a specific number of input-items - that works! 

(defun nand (a b)
  (not (and a b)))

(nand t t)
=> nil
(nand nil nil)
=> t
(nand nil t)
=> t


;;; i like to have an input perhaps like that - with any number of input-items, like lisp's AND / OR

(nand t t t t)
(nand nil t t t nil t t t nil)
...

 

Share this post


Link to post
Share on other sites

Is this what you are looking for:
 

(defun nand (&rest rest)
  (flet ((every-truep (x)
           (equal x t)))
    (not (every #'every-truep rest))))

(nand t t)
=> nil

(nand t t t t nil t)
=> t

(nand nil t t t nil t)
=> t

 

Share this post


Link to post
Share on other sites

exactly... thanks for the solution! 

i will try to CODE some XOR, NOR.... based on it! i was in trouble with the "input-format" (when it's not a list) 🤔

Share this post


Link to post
Share on other sites

Few more function which could be useful for you (part of the system):
 

(contain-itemp 'inv '(c 7b9s11 inv 1 chord))
=> t

(contain-itemp '= '(e f d s))
=> nil
(contain-itemsp '(9 8) '(0 1 11 8 10 9))
=> t
(contain-sequencep '(11 8) '(0 1 11 8 10 9))
=> t

JP

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×