a function that does binary addition of two Boolean lists. note that the lists c
ID: 3763738 • Letter: A
Question
a function that does binary addition of two Boolean lists. note that the lists cannot be equal in length.function should be in SML
fun doBinaryAddition (pbits, qbits, carryBit) =
and should return bool list * bool list * bool -> bool list a function that does binary addition of two Boolean lists. note that the lists cannot be equal in length.
function should be in SML
fun doBinaryAddition (pbits, qbits, carryBit) =
and should return bool list * bool list * bool -> bool list note that the lists cannot be equal in length.
function should be in SML
fun doBinaryAddition (pbits, qbits, carryBit) =
and should return bool list * bool list * bool -> bool list
Explanation / Answer
fun rev([])=[] | rev(h::t)=append(rev(t),h);
fun sum([],c) = [c] |
sum(h::t,c) = h+c :: sum(t,c);
fun doBinaryAddition (pbits, qbits,carryBit)
let
fun add([],q,c) = sum(rev(q),c) |
add(p,[],c) = sum(rev(p),c) |
add([],[],c) = [c] |
add(h1::t1,h2::t2,c) =
if (h1 == 1 and h2 == 1 and c == 0) then 0::add(t1,t2,1)
elseif (h1 == 1 and h2 == 1 and c == 1) then 1::add(t1,t2,1)
elseif (h1 == 0 and h2 == 1 and c == 1) then 0::add(t1,t2,1)
elseif (h1 == 1 and h2 == 0 and c == 1) then 0::add(t1,t2,1)
elseif (h1 == 0 and h2 == 0 and c == 0) then 0::add(t1,t2,0)
elseif (h1 == 0 and h2 == 0 and c == 1) then 1::add(t1,t2,0)
elseif (h1 == 0 and h2 == 1 and c == 0) then 1::add(t1,t2,0)
elseif (h1 == 1 and h2 == 0 and c == 0) then 1::add(t1,t2,0)
in
add(pbits,qbits,carryBit)
end;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.