A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
34
Task/Four-bit-adder/Clojure/four-bit-adder-1.clj
Normal file
34
Task/Four-bit-adder/Clojure/four-bit-adder-1.clj
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(ns rosettacode.adder
|
||||
(:use clojure.test))
|
||||
|
||||
(defn xor-gate [a b]
|
||||
(or (and a (not b)) (and b (not a))))
|
||||
|
||||
(defn half-adder [a b]
|
||||
"output: (S C)"
|
||||
(cons (xor-gate a b) (list (and a b))))
|
||||
|
||||
(defn full-adder [a b c]
|
||||
"output: (C S)"
|
||||
(let [HA-ca (half-adder c a)
|
||||
HA-ca->sb (half-adder (first HA-ca) b)]
|
||||
(cons (or (second HA-ca) (second HA-ca->sb))
|
||||
(list (first HA-ca->sb)))))
|
||||
|
||||
(defn n-bit-adder
|
||||
"first bits on the list are low order bits
|
||||
1 = true
|
||||
2 = false true
|
||||
3 = true true
|
||||
4 = false false true..."
|
||||
can add numbers of different bit-length
|
||||
([a-bits b-bits] (n-bit-adder a-bits b-bits false))
|
||||
([a-bits b-bits carry]
|
||||
(let [added (full-adder (first a-bits) (first b-bits) carry)]
|
||||
(if(and (nil? a-bits) (nil? b-bits))
|
||||
(if carry (list carry) '())
|
||||
(cons (second added) (n-bit-adder (next a-bits) (next b-bits) (first added)))))))
|
||||
|
||||
;use:
|
||||
(n-bit-adder [true true true true true true] [true true true true true true])
|
||||
=> (false true true true true true true)
|
||||
51
Task/Four-bit-adder/Clojure/four-bit-adder-2.clj
Normal file
51
Task/Four-bit-adder/Clojure/four-bit-adder-2.clj
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
(ns rosetta.fourbit)
|
||||
|
||||
;; a bit is represented as a boolean (true/false)
|
||||
;; a word is a big-endian vector of bits [true false true true] = 11
|
||||
;; multiple values are returned as vectors
|
||||
|
||||
(defn or-gate [a b]
|
||||
(or a b))
|
||||
|
||||
(defn and-gate [a b]
|
||||
(and a b))
|
||||
|
||||
(defn not-gate [a]
|
||||
(not a))
|
||||
|
||||
(defn xor-gate [a b]
|
||||
(or-gate (and-gate (not-gate a) b) (and-gate a (not-gate b))))
|
||||
|
||||
(defn half-adder [a b]
|
||||
"result is [carry sum]"
|
||||
(let [carry (and-gate a b)
|
||||
sum (xor-gate a b)]
|
||||
[carry sum]))
|
||||
|
||||
(defn full-adder [a b c0]
|
||||
"result is [carry sum]"
|
||||
(let [[ca sa] (half-adder c0 a)
|
||||
[cb sb] (half-adder sa b)]
|
||||
[(or-gate ca cb) sb]))
|
||||
|
||||
(defn nbit-adder [va vb]
|
||||
"va and vb should be big endian bit vectors of the same size. The result
|
||||
is a bit vector having one more bit (carry) than args."
|
||||
{:pre [(= (count va) (count vb))]}
|
||||
(let [[c sums] (reduce (fn [[carry sums] [a b]]
|
||||
(let [[c s] (full-adder a b carry)]
|
||||
[c (conj sums s)]))
|
||||
;; initial value: false carry and an empty list of sums
|
||||
[false ()]
|
||||
;; rseq is constant-time reverse for vectors
|
||||
(map vector (rseq va) (rseq vb)))]
|
||||
(vec (conj sums c))))
|
||||
|
||||
(defn four-bit-adder [a4 a3 a2 a1 b4 b3 b2 b1]
|
||||
"Returns [carry s4 s3 s2 s1]"
|
||||
(nbit-adder [a4 a3 a2 a1] [b4 b3 b2 b1]))
|
||||
|
||||
(comment
|
||||
(four-bit-adder false true true false true false true true)
|
||||
;; [true false false false true]
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue