I need to write an SML function that looks like this:
update(FLR, (x,y))
Where FLR is a finite list of tuples that looks like the following:
[(1,1),(2,4),(3,9),(4,16)]
But it can contain any number of tuples. What this update function needs to do is take in the list of tuples as the first argument and an x / y tuple as the second argument. If there is a tuple in the list that has the same x value as the one given to the function, it needs to update the y value of the list to the y value given to the function. If there is not a tuple with the x value given, the function needs to create a new tuple in the list at the appropriate location. For example, if the FLR has the values:
FLR = [(1,1),(2,4),(3,9),(4,16),(5,25)]
and the function is called as update(FLR, (2,3)); The new list should be [(1,1),(2,3),(3,9),(4,16),(5,25)]. Also, if the function is called as update(FLR, (6,36)); The new list should be [(1,1),(2,4),(3,9),(4,16),(5,25), (6,36)].
I am very new to functional programming and SML and I am not sure how to do this. I have written some memberOf functions that I'm sure I will need to use but my main concern is how to do all of this using recursion. Any help is greatly appreciated!
mapyet? It's pretty similar: you need to walk through the list, working with one element at a time, and producing a new list with elements that correspond 1:1 to elements in the original list. - ephemient