0

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}}
user3078046
  • 31
  • 1
  • 7

2 Answers2

1

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}}
yakoudbz
  • 903
  • 1
  • 6
  • 14
0

You can find the number of digits by converting to a string and taking its length:

  NumDigits = {Length {Value.toVirtualString Int 10 10}}

BTW, the ^ operator has a different meaning the Oz. You probably want the Pow function.

wmeyer
  • 3,426
  • 1
  • 18
  • 26
  • OZ is very frustrating... I figured out a couple of things If you give an Int to Value.toVirtualString, the browser doesn't show If you give a String to Length, the browser doesn't show, I tested it with actual simple strings as in {Length 'AZERTY'} the browser didn't show but when i did {Length "AZERTY"} it worked...the thing is, when i put the string i got from the toVirtualString in a variable, and give it to the function Length it doesn't work... so what should i do OMG i punched the wall so much... if you please could guide me through this i'm a complete begginer in this language – user3078046 Mar 28 '14 at 17:23