This function takes two integers and returns the list of all integers in the range [a,b]
This is the solution that I wrote.
let rec range_rec l a b =
if (a=b) then l@[b]
else range_rec (l@[a], a+1, b);;
let range a b = range_rec [] a b;;
I’m hitting an error “Error: This expression has type int list * int * int but an expression was expected of type int”. Can someone throw some light on why am I getting this error?
Thanks.
It should look like this:
What I’ve done:
looptorange_rec(l@[a], a+1, b)to(l @ [a]) (a + 1) b. The first is a triplet and the second is 3 arguments to a curried function.if (a = b) thencan be written asif a = b then.Last, the function can be made more efficient by using
::instead of@by looping “backwards”. For example like gasche have shown.