I wrote the following function in SML:
fun find_value [] item = ~1.0
| find_value ((x:string,y:real)::xt) item =
if item = x then y
else find_value xt item;
This function gets a list string * real
and a string
and returns the value of the element with the same string
. For example:
- find_value [("goo",4.0),("boo",5.0)] "goo";
val it = 4.0 : real`.
The problem is with my implementation, it will return ~1.0
if it didn't find an element with x
string in it. so if for example I have an element with value ~1.0
it will return it and I won't know if the list is empty or if the element isn't in the list or there was an element with the same string and value of ~1.0
.
Example:
find_value [("goo",~1.0),("boo",5.0)] "goo";
find_value [] "goo";
both will return val it = ~1.0
. Is there any other idea for implementation?