Four bit adder: Difference between revisions

+ Haskell
(→‎{{header|PicoLisp}}: Added PureBasic)
(+ Haskell)
Line 709:
* "<tt><-channel</tt>" reads a value from a channel. Blocks if its buffer is empty.
* "go function()" creates and runs a go-rountine. It will continue executing concurrently.
 
=={{header|Haskell}}==
Basic gates:
<lang haskell>import Control.Arrow
 
bor, band :: Int -> Int -> Int
bor = max
band = min
bnot :: Int -> Int
bnot = (1-)</lang>
Gates build with basic ones:
<lang hskell>nand, xor :: Int -> Int -> Int
nand = (bnot.).band
xor a b = uncurry nand. (nand a &&& nand b) $ nand a b</lang>
Adder circuits:
<lang haskell>halfAdder = (uncurry band &&& uncurry xor)
fullAdder (c, a, b) = (\(cy,s) -> first (bor cy) $ halfAdder (b, s)) $ halfAdder (c, a)
 
add as = foldr (\(a,b) (cy,xs) -> second(:xs)$ fullAdder (cy,a,b)) (0,[]). zip as</lang>
 
Example using add
<lang haskell>*Main> add [1,0,1,0] [1,1,1,1]
(1,[1,0,0,1])</lang>
 
=={{header|J}}==
Anonymous user