Jump to content

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)
...

 

Link to comment
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

 

Link to comment
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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy