KONADO
KONADO

Reputation: 189

Recursive OCaml function not working

I'm using the following code to try and put the average of consecutive numbers in an integer list into a new list:

 let newList = []
let rec average2 xs = 
match xs with
| [] -> newList
| x :: [] -> newList
| x :: x' :: [xs] -> append newList [((x + x')/2)] average2 x' :: [xs];;

but I keep getting the following error and don't understand why: Error: This function has type 'a list -> 'a list -> 'a list It is applied to too many arguments; maybe you forgot a `;'.

Upvotes: 0

Views: 145

Answers (1)

Palle
Palle

Reputation: 12109

You're passing the average2 function to the append function instead of calling it in the last line. Also, newList is empty and does not get mutated nor read from. You can just add a new head to the list when returning it.

Change it to

((x + x')/2) :: (average2 x' :: [xs])

Upvotes: 1

Related Questions