2016-11-11 116 views
1

假設下面的聲明,如果我想檢查它們是否都是正數,例如我如何能夠從列表ats中獲取ints?一個提示是非常感謝。F#從元組列表中獲取特定元素

type Item = string 
type ItemList = Item * int 

let ad1 = ("machine", 2) 
let ad2 = ("coffee", 1) 
let ad3 = ("washer", 2) 
let ats = [ad1; ad2; ad3; ad1; ad2] 

回答

3
let ints = ats |> List.map snd 

let allPositive = ints |> List.forall (fun x -> x > 0) 
+0

@ Khaine775它的全部應用高階函數。 :-)不管怎麼說,如果你想分解出X,你可以把它更短:'整數|> List.forall((<)0)' – s952163

+0

整潔。一個後續的問題可能是:如果我只希望自己能具有特定名稱列表'items'的'int'值,例如我想只是得到含有'的元組的'ints' coffee'? – Khaine775

+2

'讓coffeeInts = ATS |> List.filter(FST >>(=) 「咖啡」)|> List.map snd' – rmunn