0

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

6
  • Why do you have newList? It's always an empty list.
    – Palle
    Oct 21, 2017 at 13:05
  • @Palle I want to use it as a kin of variable to keep adding elements to
    – KONADO
    Oct 21, 2017 at 13:06
  • newList is constant and it's never read from in the code.
    – Palle
    Oct 21, 2017 at 13:07
  • Am I not appending thing to the list in the third line of pattern matching
    – KONADO
    Oct 21, 2017 at 13:07
  • The append function returns a new list where the first list is appended to the second list. No arguments will get mutated.
    – Palle
    Oct 21, 2017 at 13:08

1 Answer 1

1

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])
1
  • Remove newList from the argument list. It's an empty list anyway.
    – Palle
    Oct 21, 2017 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.