OCaml recursive function int list -> int -> (int list * int list) -
studying midterm , looking through old exam questions. 1 doesn't have solution posted , stumping me:
partition
: int list -> int -> (int list * int list) divides first argument 2 lists, 1 containing elements less second argument, , other elements greater or equal second argument.partition
[5;2;10;4] 4 = ([2], [5;10;4])
oh, , i'm supposed able find solution without using auxiliary function
here far i've gotten:
let rec partition l n = match l | [] -> ([], []) (* must return type *) | x :: xs -> if x < n (* append x first list, continue recursing *) else (* append x second list, continue recursing *)
normally, i'd use aux function parameter store pair of lists i'm building, can't done here. i'm bit stuck
you should use let in
construction match return value of recursive call:
let rec partition l n = match l | [] -> ([], []) | x :: xs -> let a, b = partition xs n in if x < n (x::a), b else a, (x::b);;
Comments
Post a Comment