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 x =
let accum = ([],[],0) in
let f (all_groups, current_group, size) x =
if size then ((List.rev current_group)::(all_groups),[x],1)
else (all_groups, x::current_group,size + 1) in
let (groups,last,_) = List.fold_left f acc lst in
List.rev(List.rev last::group)
I got a type error:
This expression has type bool but an expression was expected of type int 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.