Friday, January 16, 2009

Ch1, Ex6

(a) For Mul, All are zero because for the 2nd row elements would be [0*1 1*0] and subsequently all the rows will become zero.
Unfortunately I have no way to copy from oz buffer yet, so couldn't show 10th
row for Mul1. But it can be seen by running following program...

declare
GenericPascal OpList ShiftLeft ShiftRight
fun {GenericPascal Op N}
if N==1 then [1]
else L in
L={GenericPascal Op N-1}
{OpList Op {ShiftLeft L} {ShiftRight L}}
end
end

fun {OpList Op L1 L2}
case L1 of H1|T1 then
case L2 of H2|T2 then
{Op H1 H2}|{OpList Op T1 T2}
end
else nil end
end

fun {ShiftLeft L}
case L of H|T then
H|{ShiftLeft T}
else [0] end
end

fun {ShiftRight L} 0|L end

declare
fun {Mul X Y} X*Y end

declare
fun {Mul1 X Y} (X+1)*(Y+1) end

{Browse {GenericPascal Mul 10}}
{Browse {GenericPascal Mul1 10}}

No comments:

Post a Comment