declare GateMaker

fun {GateMaker F}

fun {$ Xs Ys}

fun {GateLoop Xs Ys}

case Xs#Ys of (X|Xr)#(Y|Yr) then

{F X Y}|{GateLoop Xr Yr}

end

end

in

thread {GateLoop Xs Ys} end

end

end

declare AndG OrG NandG NorG XorG

AndG ={GateMaker fun {$ X Y} X*Y end}

OrG ={GateMaker fun {$ X Y} X+Y-X*Y end}

NandG={GateMaker fun {$ X Y} 1-X*Y end}

NorG ={GateMaker fun {$ X Y} 1-X-Y+X*Y end}

XorG ={GateMaker fun {$ X Y} X+Y-2*X*Y end}

declare FullAdder

proc {FullAdder X Y Z ?C ?S}

K L M

in

K={AndG X Y}

L={AndG Y Z}

M={AndG X Z}

C={OrG K {OrG L M}}

S={XorG Z {XorG X Y}}

end

% A number is represented by x0|x1|x2|...xn|_

% where x0 is lowest order bit while xn is

% that of highest order

declare BAdder %Binary Adder

local

proc {SingleElemFullAdder X Y Z ?C ?S}

L M in

{FullAdder X|_ Y|_ Z|_ L M}

C = L.1

S = M.1

end

X Y

in

fun {BAdder Xs Ys}

fun {Adder Xs Ys C}

case Xs#Ys of (X|Xr)#(Y|Yr) then Cnext S in

{SingleElemFullAdder X Y C Cnext S}

S|{Adder Xr Yr Cnext}

end

end

in

thread {Adder Xs Ys 0} end

end

end

local

X=1|1|0|_ % 3

Y=0|0|1|_ % 4

in

{Browse 'BAdder Test: 3+4: 1|1|1|_'}

{Browse {BAdder X Y}}

end

local

X=1|1|1|0|_ % 3

Y=1|1|1|0|_ % 4

in

{Browse 'BAdder Test: 7+7: 0|1|1|1|_'}

{Browse {BAdder X Y}}

end

## Sunday, February 1, 2009

### Ch4, Ex9

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment