Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A couple of days into OCaml and programming in general and I have faced a very difficult problem (at least for me) and it is about finding the largest digit of an integer.

What I have tried:

I tried the following:

let rec maxi' x a =
  if x < 10 then x
  else maxi' (x mod 10) (x / 10 * a)
let maxi x = maxi' x 0

It returns the last digit of an integer and not the largest. Please help me with this.
Updated 21-Dec-22 14:47pm

Think about how you would do it manually: you would look at each digit in turn and check it against the "Current largest value".
If it was smaller, you would ignore it and move on to the next.
Otherwise, it would become the "Current largest value" and you'd look at the next.
Once you'd looked at them all, you have the largest in "Current largest value"

Your code doesn't do that: you don't have a "Current largest value" at all.

Think about it for a moment, and it should be pretty obvious what you need to do.
Share this answer
dumbprogrammer00 6-Nov-22 14:34pm    
Actually, that is how I have been thinking but I think I just don't have enough knowledge of the language to do it in such a way.

The following code I wrote can return the largest digit but I wanna do it without the predefined max and with the help of an accumulator (I don't even know if it is possible). The code:

let rec max_digit x = if x < 10 then x else max (x mod 10) (max_digit (x / 10))
You don't need a accumulator to collect your largest digit. But you still need to have the largest digit so far.

So this can be done like this. You can create a local function max_digit' : int -> int -> int which is the recursive one.

And you call it recursive by remove the rightmost digit and extract the the rightmost and check if it is largest of current largest and the current rightmost. Use that in recursion call.

let max_digit number =
   let rec max_digit' number maxsofar =
        max_digit' (number / 10) (max maxsofar (number mod 10))
     max_digit' number (number mod 10)

So the inner let is the first call to the local recursive function.

Could even have a local function that return a pair where it divide it. Then you can use a case were you catch (0, maxdigit) as base case. And (number, digit) as the other case.

Something like this.

let max_digit number = 
   let extract_digit number =
      (number/10), (number mod 10)
   let rec max_digit' number maxsofar =
      match extract_digit number with
      | (0, cand) -> max cand maxsofar
      | (number', cand) -> max_digit' number' (max cand maxsofar)
   max_digit' number (number mod 10)

This is the usual way of code OCaml. And yes, write the function in your REPL and test it out and build it up part by part, combined. (Notice that F# is MS version of OCaml, but with other library and some syntax changes).
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900