質問

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}}
役に立ちましたか?

解決

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}}

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top