Dear Peter,

Some comments...

On Fri, Oct 1, 2010 at 2:59 AM, Peter Breitsprecher
<[email protected]>wrote:

> I am back working on question 2.  I need to add two binary numbers.  I can
> assume they are both the same length.
>
> Here is what I have written.
>
> declare
> fun {AddDigits Xs Ys C}
>

There is a missing 'in' after Acc below:


>    local Acc
>

The following case statement will fail when either Xs or Ys is nil.  You
need a base case.


>       case Xs of H1|T1 then
>      case Ys of H2|T2 then
>         Acc = H1 + H2 + C
>      end
>       end
>

You can use (Acc div 2) and (Add mod 2) to simplify the expression below,
and remove completely the if-then-else.


>       if Acc == 0 then 0|{AddDigits T1 T2 0}
>       elseif Acc == 1 then 1|{AddDigits T1 T2 0}
>       elseif Acc == 2 then 0|{AddDigits T1 T2 1}
>       elseif Acc == 3 then 1{AddDigits T1 T2 1}
>       else
>      C|{AddDigits T1 T2 nil}
>       end
>    end
>

There is a missing 'end' here above...


>
> fun {Add1 X1 X2}
>    local Bin1 Bin2 C in
>       Bin1 = {Reverse X1}
>       Bin2 = {Reverse X2}
>       {AddDigits Bin1 Bin2 C}
>    end
> end
> fun {Reverse Xs}
>    case Xs of nil then nil
>    [] X|Xr then
>       {Append {Reverse Xr} [X]}
>    end
> end
> fun {Add Xs Ys}
>    {Reverse {Add1 Xs Ys}}
> end
>

Check the scope of your variables.  T1 and T2 are not always declared in the
places you use them...


>
>
> I have traced it in my head, and I thought it should work, but it keeps
> telling me that I have a statement at expression position.  Does anyone see
> where I went wrong.
>
> Kurt
>

Cheers,
Raphael
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to