list of list declaration 疑似错误,不知如何更正
Suspceted error in declaration of list of lists, not sure how to correct
我正在尝试获取列表列表并打印在所有列表中找到的最大值。我有一个我认为应该有效的功能,但我认为我声明值的方式导致了问题。
nested_max :: [[Int]] -> Int
nested_max [] = minBound::Int
nested_max [[x]] = x
nested_max [[x,xs]] = nested_helper [[x,xs]]
nested_helper [[x,xs]]= maximum (concat[[x,xs]])
感谢任何帮助。
除第一个模式外的所有模式都与 单例 列表匹配:具有一个元素的列表。在该列表中,您再次匹配单例列表(因此一个元素列表是一个元素列表),以及一个包含两个元素的列表)。
但是您因此不会匹配具有任意数量子列表的列表,也不匹配具有零个或两个以上元素的列表作为第一个列表的列表。
但是您不需要在列表中进行模式匹配,您可以使用:
nested_max :: [[Int]] -> Int
nested_max = helper . <strong>concat</strong>
where helper [] = minBound
helper xs = maximum xs
因此,我们首先将列表的列表连接成一个列表,然后我们检查该列表是否为空,在这种情况下我们 return minBound
,或者我们 return maximum xs
这将 return 元素的最大值。
问题是声明[[x,xs]]
,它是一个列表,其中包含两个元素的列表。这不是您想要的,因为它不是一般情况 - 在某些时候您需要一般情况来匹配您尚未匹配的所有内容。
您可以尝试 lst:lsts
匹配第一个元素,头,匹配 lst
和其他元素(如果有的话)匹配 lsts
- 这是递归的标准技术.这里不做递归,可以忽略。
您可以尝试 nlsts
一次性匹配列表列表。由于您将一项传递给 nested_helper
和 concat
,这似乎是更自然和正确的做法。
因此:
nested_max :: [[Int]] -> Int
nested_max [] = minBound::Int
nested_max [[x]] = x
nested_max nlsts = nested_helper nlsts
nested_helper nlsts = maximum (concat nlsts)
为了完整性,有一个递归版本。这使用 lst:lsts
形式:
nested_max_new :: [[Int]] -> Int
nested_max_new [[]] = minBound
nested_max_new nlst = maximum (nested_helper_new nlst)
nested_helper_new :: [[Int]] -> [Int]
nested_helper_new [] = []
nested_helper_new (lst:nlst) = (maximum lst) : nested_helper_new nlst
最后,在实际代码中我们尝试使用map
、filter
和reduce
——使用哪种函数式语言并不重要。这往往会提供最简单和最易读的代码:
nestedMax :: [[Int]] -> Int
nestedMax nlst = maximum (map (\x -> maximum x) nlst)
注意:与 Haskell 的许多基本功能一样,maximum
是部分功能,它不适用于所有输入。所以你可能更喜欢:
maxList::[Int] -> Int
maxList [] = minBound
maxList lst = maximum lst
我正在尝试获取列表列表并打印在所有列表中找到的最大值。我有一个我认为应该有效的功能,但我认为我声明值的方式导致了问题。
nested_max :: [[Int]] -> Int
nested_max [] = minBound::Int
nested_max [[x]] = x
nested_max [[x,xs]] = nested_helper [[x,xs]]
nested_helper [[x,xs]]= maximum (concat[[x,xs]])
感谢任何帮助。
除第一个模式外的所有模式都与 单例 列表匹配:具有一个元素的列表。在该列表中,您再次匹配单例列表(因此一个元素列表是一个元素列表),以及一个包含两个元素的列表)。
但是您因此不会匹配具有任意数量子列表的列表,也不匹配具有零个或两个以上元素的列表作为第一个列表的列表。
但是您不需要在列表中进行模式匹配,您可以使用:
nested_max :: [[Int]] -> Int
nested_max = helper . <strong>concat</strong>
where helper [] = minBound
helper xs = maximum xs
因此,我们首先将列表的列表连接成一个列表,然后我们检查该列表是否为空,在这种情况下我们 return minBound
,或者我们 return maximum xs
这将 return 元素的最大值。
问题是声明[[x,xs]]
,它是一个列表,其中包含两个元素的列表。这不是您想要的,因为它不是一般情况 - 在某些时候您需要一般情况来匹配您尚未匹配的所有内容。
您可以尝试 lst:lsts
匹配第一个元素,头,匹配 lst
和其他元素(如果有的话)匹配 lsts
- 这是递归的标准技术.这里不做递归,可以忽略。
您可以尝试 nlsts
一次性匹配列表列表。由于您将一项传递给 nested_helper
和 concat
,这似乎是更自然和正确的做法。
因此:
nested_max :: [[Int]] -> Int
nested_max [] = minBound::Int
nested_max [[x]] = x
nested_max nlsts = nested_helper nlsts
nested_helper nlsts = maximum (concat nlsts)
为了完整性,有一个递归版本。这使用 lst:lsts
形式:
nested_max_new :: [[Int]] -> Int
nested_max_new [[]] = minBound
nested_max_new nlst = maximum (nested_helper_new nlst)
nested_helper_new :: [[Int]] -> [Int]
nested_helper_new [] = []
nested_helper_new (lst:nlst) = (maximum lst) : nested_helper_new nlst
最后,在实际代码中我们尝试使用map
、filter
和reduce
——使用哪种函数式语言并不重要。这往往会提供最简单和最易读的代码:
nestedMax :: [[Int]] -> Int
nestedMax nlst = maximum (map (\x -> maximum x) nlst)
注意:与 Haskell 的许多基本功能一样,maximum
是部分功能,它不适用于所有输入。所以你可能更喜欢:
maxList::[Int] -> Int
maxList [] = minBound
maxList lst = maximum lst