I've wrote the following function to find a given item "x" in a given list "lst" and return its index if it's found, otherwise it would return an error:
exception Failure of string
let rec func x lst c = match lst with
| [] -> raise(Failure "Not Found")
| hd::tl -> if (hd=x) then c else func x tl (c+1)
let find x lst = func x lst 0
The function is fully working, I'm just wondering what is the memory consumption of it? Meaning does the memory consumption depend on the length of the list? or is it O(1)?
If it's not O(1) can someone please let me know what should I do to make it so?
Thank you