Match pattern in tuple list

If I want to add all the elements of a list of tuples, I get an error with the following

let rec addTupLst (xs: 'a * 'a list) =
    match xs with
    | (a, b) :: rst -> a + b + (addTupLst rst)
    | _ -> 0

addTupLst [(1, 2)]

I get a warning

error FS0001: this expression is expected to be of type 'a *' list
but there is type 'b list here

Is it impossible to match the pattern in the tuple list this way, or is there another error?

+4
source share
2 answers

You just forgot a couple of partners

let rec addTupLst (xs: ('a * 'a) list) =
     match xs with
     | (a, b) :: rst -> a + b + (addTupLst rst)
     | _ -> 0

addTupLst [(1, 2)]
+4
source

The problem is that you declare the function as host 'a * 'a list, but what you really want to write is ('a * 'a) list.

, , () . list<'a * 'a>.

+2

Source: https://habr.com/ru/post/1658450/


All Articles