First, consider your code and annotate all types:
let findInList source target = let itemFound = seq { for n in source do yield target |> List.filter (fun (x,y) -> x = n) } |> Seq.head itemFound
The yield List.Filter ... means that you are creating a sequence of lists: seq<list<'a * 'b>> .
The Seq.head takes the first element from your list of lists: list<'a * 'b> .
Thus, the whole function returns list<'a * 'b> , which is clearly not suitable for your function. I think you intended to write something like this:
let findInList source target = let itemFound = target // list<'a * 'b> |> List.filter (fun (x,y) -> x = n) // list<'a * 'b> |> Seq.head // 'a * 'b itemFound // function returns 'a * 'b
There are many ways to get the desired results. Your code is halfway there. Instead of manually filtering, I recommend using the built-in val Seq.find : (a' -> bool) -> seq<'a> -> 'a method val Seq.find : (a' -> bool) -> seq<'a> -> 'a :
let findAge l name = l |> Seq.find (fun (a, b) -> a = name) |> snd
Or you can try using another data structure, such as Map<'key, 'value> :
> let namesAndAges = [ ("john", 10); ("andrea", 15) ] |> Map.ofList;; val namesAndAges : Map<string,int> = map [("andrea", 15); ("john", 10)] > namesAndAges.["john"];; val it : int = 10
If you want to write it manually, try this with a seq expression:
let findInList source target = seq { for (x, y) in source do if x = target then yield y} |> Seq.head