suggestion: nand, nor, and xor functions

Q&A's, tips, howto's
Post Reply
CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

suggestion: nand, nor, and xor functions

Post by CaveGuy »

The 3 inverse logic functions :
(nand ...)
(nor ..... )
(xor ....) would be nice as primitives :)

BTW. Hi guys I have been off on a project and just got back.
I hope to get some time in the next week or two to clean it up
an example or two :)
Bob the Caveguy aka Lord High Fixer.

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: suggestion: nand, nor, and xor functions

Post by CaveGuy »

Its been a few years yet still no nand nor or xor :(
really wish I had them right now as I attempt to model a complex Boolean.

count this as a recalled vote from the past ....
Bob the Caveguy aka Lord High Fixer.

TedWalther
Posts: 606
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: suggestion: nand, nor, and xor functions

Post by TedWalther »

If I have to guess, I'd say there is no nand, nor, and xor because xor is already present with the ^ operator (like in C), nand and nor are easily composed from (not (and)) and (not (or)). Or something like that. If you have a use case where this is a performance bottleneck, like a tight inner loop for doing crypto, it should be easy enough to add them as C primitives, who knows, maybe the compiler will make them a bit more efficient.

Now, having a standard library that included such functions, that could be a good thing. Maybe newlisp could use a standard library that you (load) at start time, right now everyone has init.lsp, but that could be standardized a bit. My own init.lsp has a bunch of small functions that would be really nice to have as a standard library, for instance, "bandpass" clips values to fit within a given range. Also I've defined the unicode lambda character so it has syntax very similar to lambda definitions in the lambda calculus, very compact.

Anyhow, add these to your init.lsp and you have your nand, nor, xor, and xnor.

Code: Select all

(define (nand a b) (not (and a b)))
(define (nor a b) (not (or a b)))
(define (xor a b) (if (nand a b) (or a b) nil))
(define (xnor a b) (not (xor a b)))
And I went ahead and made you some bitwise versions:

Code: Select all

;; bitwise versions:
(define (~& a b) (~ (& a b))) ; nand, bitwise
(define (~| a b) (~ (| a b))) ; nor, bitwise
;; xor is already in the language as ^
(define (~^ a b) (~ (^ a b))) ; xnor, bitwise
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: suggestion: nand, nor, and xor functions

Post by CaveGuy »

You make some good points, here are a few limp counterpoints :)

I can do an (and a b c d e ... I am looking for a multy input (nand a b c d ...

Almost everything I do gets linked to an exe which makes init.lsp not only useless but dangerous to have it effect my codeing and testing environment and not my runtime code.

I agree just about everything can be modeled or simulated in lisp. many years back I did a tristate logic project using NaN [not a number]as a third state. an atom could have a value and be true, nil indicated unknown and not false, while NaN indicated a state known to be false (a hard nil) or was insignificant and to be ignored. at the time I traded off nand and nor for NaN?. I only brought nand and nor up again 15 years later because I needed one with 5 inputs last night.

back under my rock :)
Bob the Caveguy aka Lord High Fixer.

TedWalther
Posts: 606
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: suggestion: nand, nor, and xor functions

Post by TedWalther »

CaveGuy wrote:I can do an (and a b c d e ... I am looking for a multy input (nand a b c d ...
Please show the output for multy (nand a b c d) Is the (not ...) applied as the last operation, or is it done more as a "fold" operation like (nand (nand (nand a b) c) d) ?
Almost everything I do gets linked to an exe which makes init.lsp not only useless but dangerous to have it effect my codeing and testing environment and not my runtime code.
That design pattern is easy to defang. What I do is, in every file, it is just definitions, ending with a (main) routine.

So, I run my application, (save "myprogram.lsp") the whole environment, and append a call to (main)(exit) to myprogram.lsp, then run newlisp -x to "compile" it. That way I don't have to do anything special to go through the dependancy list; the program already ran and loaded whatever will be loaded; everything is saved as one file. (save) doesn't save the actual source of the file; it saves all the definitions. As long as your top level program is saved in a routine (such as main) you are good to go. It takes a little bit of discipline, but not much.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

TedWalther
Posts: 606
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: suggestion: nand, nor, and xor functions

Post by TedWalther »

Also, if you want multy versions, show me the output for nand, nor, and xnor. The main thing I need to know is, is "not" applied as the last operation to regular standard multy and, nor, xor, or is it a chain of nand, nor, xnor operations.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: suggestion: nand, nor, and xor functions

Post by CaveGuy »

I ended up using a chain. looks ugly but it worked. What I really want is to go back to the pre1999 newlisp before the dos console code was taken out. And while I am wishing, a quick and easy inter-process communication for windows. using the file system for inter-process communication is so 80'ish but it still works :)
Bob the Caveguy aka Lord High Fixer.

TedWalther
Posts: 606
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Re: suggestion: nand, nor, and xor functions

Post by TedWalther »

Chaining them together would be easier with something like foldr or foldl from Haskell. Lutz? You reading this? Also, the "thrush" operator from Clojure is pretty handy.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence. Nine months later, they left with a baby named newLISP. The women of the ivory towers wept and wailed. "Abomination!" they cried.

CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

Re: suggestion: nand, nor, and xor functions

Post by CaveGuy »

If I was still in my autolisp days I would have taken a different approach and used a (mapcar
oh the fun we had with (mapcar '(apply combinations on 3d point lists :)
Bob the Caveguy aka Lord High Fixer.

Post Reply