longestProductLen :: [(Barcode, Item)] -> Int
longestProductLen = maximum . map (length . fst . snd)
.是函数组合,所以maximum. map f表示映射f,然后取最大值,比如f是(+5),那么我们得到
( maximum .map (+5) ) [1,2,3,4]
= maximum (map (+5) [1,2,3,4])
= maximum [6,7,8,9]
= 9
在您提供的代码中,. 也用于(length . fst . snd)。
请注意,由于longestProductLen :: [(Barcode, Item)] -> Int,如果我们将f 映射到该列表上,f 必须接受(Barcode, Item) 类型的数据。
它需要snd,它给它一个项目,然后是fst,所以它必须是type Item = (Product,???)。我不知道什么???是,但这对您的功能无关紧要。我猜Double。
接下来我们采用length,所以type Product = [????]。我怀疑它是[Char],即字符串,但无论如何,我们可以接受它的长度。
让我们通过一些示例数据来解决这个问题:
(length . fst . snd) ("|| ||| | ||| | || |||| | |", ("Gruyere",1.05))
= (length . fst) (snd ("|| ||| | ||| | || |||| | |", ("Gruyere",1.05)) )
= (length . fst) ("Gruyere",1.05)
= length ( fst ("Gruyere",1.05) )
= length "Gruyere"
= 7
把它放在一起给出
longestProductLen [("|| ||| | ||| | || |||| | |", ("Gruyere",1.05)),
("| ||| || ||| || |||| || |", ("Emmental",0,97)),
("||||| ||| ||| || | || |||", ("Gouda",1,21))]
= maximum . map (length . fst . snd)
[("|| ||| | ||| | || |||| | |", ("Gruyere",1.05)),
("| ||| || ||| || |||| || |", ("Emmental",0,97)),
("||||| ||| ||| || | || |||", ("Gouda",1,21))]
= maximum [7,8,5]
= 8
所以我们发现最长的产品长度是 8(来自 Emmental)。