0
votes

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 `;'.

1
Why do you have newList? It's always an empty list. - Palle
@Palle I want to use it as a kin of variable to keep adding elements to - KONADO
newList is constant and it's never read from in the code. - Palle
Am I not appending thing to the list in the third line of pattern matching - KONADO
The append function returns a new list where the first list is appended to the second list. No arguments will get mutated. - Palle

1 Answers

1
votes

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])