Dear Kurt,

If I understand correctly, your function AddDigits adds two digits plus a
carry digit.  It should therefore return a digit and a carry digit,
shouldn't it?

Another remark: your function Add has no base case.  It will always try
match non-empty lists.  Note that you can combine the pattern matching on
both lists simultaneously: just consider a value like B1#B2.  Your cases
will look like:

   case B1#B2
   of (H1|T1)#(H2|T2) then
      % two non-empty lists
   [] nil#nil then
      % two empty lists
   else
      % two lists of different sizes
   end

Cheers,
Raphael

On Tue, Sep 28, 2010 at 12:18 AM, Peter Breitsprecher <[email protected]
> wrote:

> I have an assignment to create a program that will add two Binary Numbers.
> I can assume that both numbers are of the same length, and that both are
> binary in the form of {Add [0 1 0 1 1 0] [0 0 1 0 1 1]}.  I have developed a
> program that will add the two numbers together, but it doesn't give me a
> binary result.  I need to write another function that will deal with adding
> the two numbers and the carry over.  It is the Carry over I'm having trouble
> with.  I can grasp how the program I wrote manipulates the stack, but i'm
> not sure how the new function will throw the carry over into the preceding
> stack result and then add them again.
>
> Can anyone help me.  I would prefer to not be given the answer right yet,
> but at least help lead me in the right direction.  Here is what I have so
> far.
>
> declare
> fun {Add B1 B2}
>    if {Length B1} == {Length B2} then
>       case B1 of H1|T1 then
>      case B2 of H2|T2 then
>         H1 + H2|{Add T1 T2}
>      end
>       end
>    else
>       raise domainError(0)
>       end
>    end
> end
>
> declare
> fun {AddDigits X Y C}
>       if X + Y + C == 0 then 0 else 1 end
> end
>
> The AddDigits function is written to simply add H1 and H2 together and
> place the result in the stack(it isn't finished yet, but this is a start and
> I will build upon it to complete it)  I will replace the H1+H2|{Add T1 T2}
> with {Adddigits X Y C}|{Add T1 T2} eventually.
>
> I also know the AddDigits function needs to be a function within a
> function, but i'm not sure how exactly it is to work.  Any help would be
> appreaciated.
>
> One last thing about this forum, how do I reply to a thread I create if I
> need more information?  I haven't figured that out yet either...
>
>
> --
> Kurt Breitsprecher
> (807) 474-9601
> [email protected]
>
>
>
> _________________________________________________________________________________
> mozart-users mailing list
> [email protected]
> http://www.mozart-oz.org/mailman/listinfo/mozart-users
>
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to