I am trying to take a desired number of elements out of an input list in OCAML. For example, I call my function as “take” which takes two parameters “my_list” and “x”. Here it is:
let take my_list n = let acc = (,0) in let f (current_list, size) x = if n = 0 || n < 0 || size > n then (,0) else if size < n then ((x::current_group),size + 1) in let (final_list,_) = List.fold_left f acc n in List.rev(List.rev final_list)
I got a type error:
This expression has type 'a * 'b but an expression was expected of type 'c list Why is this the case?
I am guessing there is something I need to do with my if…else statement. My reason is that if the desired number of elements I want to take out of my list is true (i.e implement this code if I want to take 3 elements out of the list, no 2), then proceed. Otherwise, keep adding the elements to the current list until it is filled with the desired number of elements I want to take out.
Thank you for any input.