Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 528

Max int in list ocaml

$
0
0

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 = xresult = 0 + old_max

A new max is found :

new_max= yresult = 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 ;;


Viewing all articles
Browse latest Browse all 528

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>