"ocaml_beginners"::[] help in ocaml

View: New views
2 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] help in ocaml

by codergen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello I am new to ocaml and was wondering how I would calculate the centroid of the list of list of integers corresponding to some negative values.We need to define a recursive function involving tuple where the first argument would be image 1 and the second would be image 2.
plz help


      Yahoo! India has a new look. Take a sneak peek http://in.yahoo.com/trynew

[Non-text portions of this message have been removed]


Re: "ocaml_beginners"::[] help in ocaml

by artemis01 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sumedha jain a écrit :
> Hello I am new to ocaml and was wondering how I would calculate the
> centroid of the list of list of integers corresponding to some negative
> values.We need to define a recursive function involving tuple where the
> first argument would be image 1 and the second would be image 2.
> plz help

Hello,

I'm not sure I completely understood your question - especially I don't
see what you mean by images 1 and 2 - but the following code computes
the centroid of a list of integers:


let centroid l =

  let rec sum_length ((sum,length) as acc) = function
    | [] -> acc
    | x::xs -> sum_length (sum+x,length+1) xs in

  let (sum,length) = sum_length (0,0) l in
  (float_of_int sum) /. (float_of_int length);;


The interest is that it is tail-recursive.

Hope this helps,
--
Chantal KELLER