I want to find the max element in a int list.
The idea is to call find_max
only once and let support
do the dirty job.
The function support
returns an int, the first value is 0 then when a new max
is found it's value is saved, added to the result ,the previous max is changed and it's value deducted from the result.
This because :
old_max = x
result = 0 + old_max
A new max is found :
new_max= y
result = result - oldmax + new_max
So I'll save the value of new_max
:
0 + old_max - old_max + new_max = new_max`.
Obviously the code above is explicative, this is my real code :
let find_max random =
let rec support rlist max =
if rlist==[] then 0
else
if (List.hd rlist) > max
then -max + (List.hd rlist) + support (List.tl rlist) (List.hd rlist)
else support (List.tl rlist) max ;;
let return = support random 0 0 ;
!return;;
let a = [1;2;3];
print_string "max element in list is : "
print_int (find_max a);
print_string "\n"
The error is on line 9 !return;;
, syntax error (obviously :/ ) on ;;
let ... in ...
syntax properly. This was explained quite well in an answer to your first question: stackoverflow.com/a/62488711/20371 . I think you had better go and read that carefully to internalize it, and familiarize yourself with the basic syntax rules. – Yawar