What is NOR logical operator? - language-agnostic

Is nor:
!(a or b)
!a or !b
!(a and b)
something else?

!(a or b)
see http://en.wikipedia.org/wiki/Logical_NOR for more:
In boolean logic, logical nor or joint denial is a truth-functional operator which produces a result that is the negation of logical or. That is, a sentence of the form (p NOR q) is true precisely when neither p nor q is true—i.e. when both of p and q are false. In grammar, nor is a coordinating conjunction...

!(a or b)

NOR(a, b) is defined to be NOT(OR(a, b)) which is !(a or b) in infix notation. By De Morgan's Laws, this is also equivalent to (!a) and (!b).

((a NAND a) NAND (b NAND b)) NAND ((a NAND a) NAND (b NAND b)) if you want to be circuit fabrication friendly. :)

your first alternative: !(a or b)
which happens to be equivalent to !a and !b

Related

Scheme - function to count occurrences of atom in argument

I'm new to scheme and trying to learn functions. I want to create a function where I can count all occurrences of the atom "a" in an argument. This is what I have written so far. I'm not sure what to write in the third condition. Please help out.
(define (count-a arg)
(COND
((null? arg) 0)
((eq? arg 'a) 1)
((list? arg) ( ___ + ( ___ count-a arg)))
(else 0)
))
This is the output i want:
(count-a 'a)
1
(count-a 'aa)
0
(count-a '(a))
1
(count-a '(ab c)
0
(count-a '(a (b c) (c (d a) a) (((a b)))))
5
(___ + ( ___ count-a arg)) doesn't seem right to me. Remember Scheme is a prefix language. It's (+ 1 2 3) instead of 1 + 2 + 3.
In the third one you have two lists parts. eg. '(a a) so you need to call count-a on the car and on the cdr of the list and add the results together. eg. (count-a '(a a)) should give the same result as (+ (count-a 'a) (count-a '(a))
Good luck
Welcome to Stack Overflow!
Solution:
(define (count-a arg)
(cond ((null? arg) 0)
((not (pair? arg))
(if (equal? arg 'a)
1
0))
;; At this point we know arg is a pair, and we recurse.
(else (+ (count-a (car arg)) (count-a (cdr arg))))))
Test:
Here are the tests you specified, compressed into a single line.
;; Chibi-Scheme REPL - other implementations might look different.
> (map count-a '(a aa (a) (ab c) (a (b c) (c (d a) a) (((a b))))))
(1 0 1 0 4)
You didn't say what you want to happen if arg is a dotted pair. But let's check it out just for fun.
> (map count-a '((ab . '()) (ab . a) (a . a)))
(0 1 2)
So when I run your last test on my code, it yields 4 where you expected 5. I maintain that my code is correct and your test specification is incorrect, as there are only four instances of 'a in the argument of count-a.
Discussion:
You specified in your headline that the thing the count-a function will be counting is an atom. So you have to check whether arg is an atom or not. Since Scheme doesn't have an atom? function, I'll assume that an atom is anything that's not '() and not a pair. (This is consistent with the atom function of Common Lisp, as well as other Lisp dialects.)
Since your code already deals with arg being '(), we only have to deal with arg being, or not being a pair.
If arg is not a pair and (equal? arg 'a), then (count-a arg) equals 1. Else, it equals 0.
If arg is a pair, we can recurse into car arg) and (cdr arg), however deeply nested arg may be, and increase our count whenever we find an atom that equals a.
Hope that helps.

Typed lambda functions in Common Lisp

I don't know of any practical uses for this, it just came to my mind whether there is any thing comparable to defmethod to defun for lambda? Something like this
(defmacro tlambda (args &body body)
(let* ((gf-name (subseq (write-to-string (gensym)) 2))
(gf-sym (read-from-string gf-name)))
`(progn (defmethod ,gf-sym ,args ,#body)
(prog1 (symbol-function ',gf-sym) (unintern ',gf-sym)))))
(tlambda ((x fixnum))) ;#<STANDARD-GENERIC-FUNCTION #:G759 (1)>
(funcall (tlambda ((x fixnum)) (* x 2)) 4) ;8
(funcall (tlambda ((x list)) (second x)) '(a s d f)) ;S
(funcall (tlambda ((x string)) (string-upcase x)) "lambda") ;"LAMBDA"
I do not think that this makes sense in the shape you show. What should happen if the type doesn't match? If you just want to check types, use check-type.
Defmethod and defun are not really comparable, by the way. Defun registers a function, while defmethod adds a method to an existing (though maybe implicitly created) generic function. The types that you use in a method definition are used to dispatch (runtime polymorphism) to the right method upon invocation of the generic function. The mechanisms for this dispatch are a bit expensive when constructed, so you probably shouldn't try to do that on the fly (something like a generic-lambda) or transiently (something like a method-let).
Instead, use (e/c)typecase and similar for ad hoc dispatch, and check-type for checking types. There are also libraries for pattern-based polymorphism (e. g. optima, trivia), which you could use for more elaborate cases.
I agree with Svante that you probably don't want this. But if you did want it the way you are doing it is very confused: I simply don't understand what you are doing with gf-sym and gf-name but it represents some quite serious confusion about symbols I think (and is almost certainly unsafe). Instead you could do something like this:
(defmacro tlambda (&body cases)
(let* ((gf-name (gensym)))
`(progn
,#(mapcar (lambda (case)
`(defmethod ,gf-name ,#case))
cases)
(symbol-function ',gf-name))))
And now
> (let ((l (tlambda
((x y) (cons x y))
((x (y integer))
(declare (ignore x)) y))))
(values (funcall l 'a 'b)
(funcall l 'a 1)))
(a . b)
1
I'm not sure whether the objects created by tlambda can be garbage-collected: it may be they can.

Construct circuit using only NAND and NOT GATES

In the image above, circuit is Sum of Products
(B’+D’) (A+D) (A+C)
The image below is my attempt on using NAND and NOT gates only. However, my senses is telling me that I am doing it wrongly. Please help!
The first circuit actually implements
which is this circuit:
Using De Morgan's Laws, this is equivalent to
which is this circuit:
This Python program can be used to compare the circuits:
import itertools
# Create all the possible input combinations
x = (True, False)
comb = set(itertools.product(x, x, x, x))
# AD + AC + B'D'
def c1(a, b, c, d):
return ((a and d) or (a and c) or ((not b) and (not d)))
# ((AD)'(AC)'(B'D')')'
def c2(a, b, c, d):
return not ((not (a and d)) and (not (a and c)) and (not ((not b) and (not d))))
# For each input, verify that the results are the same
for x in comb:
r1 = c1(*x)
r2 = c2(*x)
if r1 != r2:
print "Error: Input %s produced %s != %s" % (x, r1, r2)
You can replace all AND gates with NAND gates and just negate the result. I can see that you negated the inputs, which is wrong because:
(ab)' != a'b'
As an example think about signals (a, b) = (1, 0). If you negate them and calculate output, you get 0. If you first calculate and then negate output, you get 1.
About the OR gate:
a + b + c -> ((a + b + c)')' -> (a'b'c')'
So OR gate is NAND gate with all signals negated.

Lisp function which adds parentheses in a certain way

I'm trying to write a function which adds parentheses like this: (parens '(a b c d e)) returns (a (b (c (d (e))))). I'm just not seeing the pattern very well. What I have so far just returns a list with parentheses around each element. I can't seem to figure out how to make it look like that.
(DEFUN PARENS (L)
(COND ((NULL L) NIL)
(T (CONS (LIST (CAR L)) (PARENS (CDR L))))))
There are no parenthesis in a list. You're starting with a list of five elements, (a b c d e), and getting back a list of two elements, (a (b (c (d (e))))). The first element is a, and the second is another list, (b (c (d (e)))).
It's very easy to get close to this using reduce:
CL-USER> (reduce 'list '(a b c d e) :from-end t)
(A (B (C (D E))))
You can think of reduce as "injecting" the function list into (a b c d e) to produce
(list a (list b (list c (list d e))))
It's almost what you want. You actually want:
(list a (list b (list c (list d (list e)))))
How would you produce that? You can recurse down the list, and for each sublist (x . ys) you want to return (list x (recurse ys)), with the exception being when ys is (). You don't want to recurse into (), because you don't want a list with two elements, you actually want nothing. So the trick is stop recursing earlier than you typically do with a list. Thus:
(defun parens (l)
(cond
((endp l) '())
((endp (rest l)) l)
((list (first l) (parens (rest l)))))) ; *
CL-USER> (parens '(a b c d e))
(A (B (C (D (E)))))
CL-USER> (parens '(a b))
(A (B))
CL-USER> (parens '(a))
(A)
CL-USER> (parens '())
NIL
*Omitting the t test in the last clause is intentional. If there are no body forms in a cond clause, then the value of the test is returned. Thus (list …) serves both as the test form and the value form.
We can actually clean that up a little bit. The case of ((endp l) '()) could be ((endp l) l) since l is the empty list. But that means that in both the first and second cases, we can return l. We can call (rest '()) in Common Lisp and get back (), so (rest l) will be () when l is something like (e) and when l is (). This means that we can use:
(defun parens (l)
(cond
((endp (rest l)) l)
((list (first l) (parens (rest l))))))
If we just have one test, though, we might as well just use if:
(defun parens (l)
(if (endp (rest l))
l
(list (first l) (parens (rest l)))))
You can actually do it with reduce and some special consideration for the end:
(defun unflatten (list)
(reduce #'list list
:from-end t
:end (1- (length list))
:initial-value (last list)))
Note that last returns a list of the last n (default 1) elements.

scheme2lisp::define function and pass it as parameter

I need to translate some code from Scheme to Common Lisp. Now, I have something like this:
(defun sum (term a next b)
(if (> a b)
0
(+ (term a) (sum term (next a) b))))
(defun sum-int (a b)
(defun (ident x) x)
(sum ident a 1+ b))
but it produces errors.
*** - DEFUN: the name of a function must be a symbol, not (IDENT X)
Help me plese.
Thanks
upd
original code:
(define (sum term a next b)
(if (> a b)
0
(+ (term a) (sum term (next a) b))))
(define (sum-int a b)
(defun (identity x) x)
(define identity a 1+ b))
(defun sum (term a next b)
(if (> a b)
0
(+ (funcall term a) (sum term (funcall next a) next b))))
(defun sum-int (a b)
(flet ((ident (x) x))
(sum #'ident a #'1+ b)))
Just another CL take with FLET (untested).
I think I got the gist of what you were looking for...
(defun sum (term a next b)
(if (> a b)
0
(+ (funcall term a) (sum term (funcall next a) next b))))
(defun ident (x) x)
(defun sum-int (a b)
(sum #'ident a #'1+ b))
Or more CLish, without explicitly defuning ident:
(defun sum-int (a b)
(sum (lambda (x) x) a #'1+ b))
You need #' quoting to get a function object since CL has separate namespaces for functions (defined with defun) and variables.