Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Change element in list and return full new list

Tags:

f#

I have a list of type (string * (int * int)) list. I want to be able to search through the list, finding the right element by it's string identifier, do a calculation on one of the ints, and then return the full, modified list.

Example:

Given a list

let st = [("a1",(100,10)); ("a2",(50,20)); ("a3",(25,40))]

I'm trying to make a function which gets one of the elements and subtracts number from one of the ints in the tuple.

get ("a2",10) st 
//Expected result: st' = [("a1",(100,10)); ("a2",(40,20)); ("a3",(25,40))]

I feel I'm almost there, but am a little stuck with the following function:

let rec get (a,k) st =
    match st with
    | (a',(n',p'))::rest when a'=a && k<=n' -> (n'-k,p')::rest
    | (a',(n',p'))::rest                    -> (n',p')::get (a,k) rest
    | _                                     -> failwith "Illegal input"

This returns [("a2",(40,20)); ("a3",(25,40))] and is thus missing the first a1 element. Any hints?

like image 284
Khaine775 Avatar asked Dec 17 '17 16:12

Khaine775


People also ask

What does ⟨F⟩ mean?

This sound is usually considered to be an allophone of /h/, which is pronounced in different ways depending upon its context; Japanese /h/ is pronounced as [ɸ] before /u/. In Welsh orthography, ⟨f⟩ represents /v/ while ⟨ff⟩ represents /f/. In Slavic languages, ⟨f⟩ is used primarily in words of foreign (Greek, Latin, or Germanic) origin.

What does the letter F mean in math?

In countries such as the United States, the letter "F" is defined as a failure in terms of academic evaluation. Other countries that use this system include Saudi Arabia, Venezuela, and the Netherlands. In the hexadecimal number system, the letter "F" or "f" is used to represent the hexadecimal digit fifteen (equivalent to 15 10 ).

What does F stand for in the Etruscan alphabet?

In the Etruscan alphabet, 'F' probably represented /w/, as in Greek, and the Etruscans formed the digraph 'FH' to represent /f/.

Is the letter F doubled at the end of words?

It is often doubled at the end of words. Exceptionally, it represents the voiced labiodental fricative / v / in the common word "of". F is the twelfth least frequently used letter in the English language (after C, G, Y, P, B, V, K, J, X, Q, and Z ), with a frequency of about 2.23% in words.


1 Answers

Lists are immutable, so if you want to "change one element" you are really creating a new list with one element transformed. The easiest way to do a transformation like this is to use List.map function. I would write something like:

let updateElement key f st = 
  st |> List.map (fun (k, v) -> if k = key then k, f v else k, v)

updateElement is a helper that takes a key, update function and an input. It returns list where the element with the given key has been transformed using the given function. For example, to increment the first number associated with a2, you can write:

let st = [("a1",(100,10)); ("a2",(50,20)); ("a3",(25,40))]

st |> updateElement "a2" (fun (a, b) -> a + 10, b)
like image 120
Tomas Petricek Avatar answered Sep 19 '22 15:09

Tomas Petricek