2016-11-11 112 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