I'm using 3 functions to find the largest number in a list and print it's length.
Sample input would be maxL [5, 199, 3000, 63];
fun max(a,b) = if a>b then a else b; fun maxL(L) = if L= then 0 else let val largest = max(hd(L), maxL(tl(L))) in num_digits largest end; fun num_digits 0 = 0 |num_digits 1 = 1 | num_digits x = num_digits(x div 10) + 1;
- maxL [5, 199, 3000, 63]; val it = 1 : int
Here I modified the program to just print largest to verify it is finding largest
- use "hw4.sml"; [opening hw4.sml] val num_digits = fn : int -> int val max_digits = fn : int list -> int val max = fn : int * int -> int val maxL = fn : int list -> int val it = () : unit - maxL [5, 199, 3000, 63]; val it = 3000 : int
Here I'm testing that 3000, or largest, is actually printing off the correct length which it is.
- num_digits 3000; val it = 4 : int
My question is: Why does it print off 1 when I call
num_digits largest, but if I call
num_digits 3000 it prints 4?