Search code examples
ozmozart

Number of digits


I'm programming a function In Mozart-Oz that returns the mirror of a number, for example

Mirror(1234) will return 4321

So anyway I have the idea how to, but I'm stuck because I need an in-built function that returns the number of digits (returns an integer) of an integer.

I tried the {Length X} function but I have no idea what it returns...

Here's my code (that doesn't work) to understand the context of my problem.

declare
fun {Mirror Int Acc}
if Int==0 then Acc
else {Mirror (Int div 10) (Int mod 10)*(10^({Length Int}-1))+Acc}end
end

{Browse {Mirror 1234 0}}

Solution

  • I would have done that:

    declare
    fun{Mirror X Y}
       if X==0 then Y
       else {Mirror (X div 10) (X mod 10)+Y*10}
       end
    end
    {Browse {Mirror 1234 0}}
    

    or, if you want only one argument:

    declare
    fun{Mirror X}
       fun{Aux X Y}
          if X==0 then Y
          else {Aux (X div 10) (X mod 10)+Y*10}
          end
       end
    in
       {Aux X 0}
    end
    {Browse {Mirror 1234}}