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. Commented Oct 21, 2017 at 13:05
  • @Palle I want to use it as a kin of variable to keep adding elements to Commented Oct 21, 2017 at 13:06
  • newList is constant and it's never read from in the code. Commented Oct 21, 2017 at 13:07
  • Am I not appending thing to the list in the third line of pattern matching Commented 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. Commented 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]) 
Sign up to request clarification or add additional context in comments.

1 Comment

Remove newList from the argument list. It's an empty list anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.