List 如何对列表中的元组执行函数?哈斯克尔
我有一个叫做image的类型,它包含一组元组,它们是坐标。我试着做一个列表理解,在这里我可以打印出x或y坐标列表的最大值和最小值List 如何对列表中的元组执行函数?哈斯克尔,list,haskell,tuples,List,Haskell,Tuples,我有一个叫做image的类型,它包含一组元组,它们是坐标。我试着做一个列表理解,在这里我可以打印出x或y坐标列表的最大值和最小值 example :: Image -> (Int,Int) example e = [ (j,k) | (x,y) <- e, j <- (maximum x), k <- (minimum y)] 对于j和k,更新的答案是: 以下是图像和点的定义: type Image = [Point] type Point = (Int,Int)
example :: Image -> (Int,Int)
example e = [ (j,k) | (x,y) <- e, j <- (maximum x), k <- (minimum y)]
对于j和k,更新的答案是:
以下是图像
和点
的定义:
type Image = [Point]
type Point = (Int,Int)
以及应如何编写示例
:
example :: Image -> (Int,Int)
example e = (maximum xs, minimum xs)
where xs = map fst e -- xs = list of the x coordinates
ys = map snd e -- ys = list of the y coordinates
原始答复:
使用让
:
example e = [ (j,k) | (x,y) <- e, let j = maximum x, let k = minimum y]
示例e=[(j,k)|(x,y)您能展示一些示例代码,以便我们能更好地帮助您吗?因此您有类似[(Int,Int)]
的内容,您需要类似([Int],[Int])
?使用解压
。显示错误本身也会非常有帮助。Haskell严格的键入可以非常详细地说明给定语句无效的原因。这给了我以下错误:image.hs:69:51:无法匹配预期的类型“[a]'与实际类型'Int'相关的绑定包括'maxium'的第一个参数中的j::a(绑定在image.hs:69:39),即表达式中的'x':maximum ximage
的定义是什么?定义了(Int,Int)的类型点列表。因此它的[Point]谢谢,现在开始工作。如果我也想得到snd的最大值和最小值,我该怎么做?使用where两次会给我一个错误您不会使用where两次。答案已更新-请参阅如何定义ys
。
example :: Image -> (Int,Int)
example e = (maximum xs, minimum xs)
where xs = map fst e -- xs = list of the x coordinates
ys = map snd e -- ys = list of the y coordinates
example e = [ (j,k) | (x,y) <- e, let j = maximum x, let k = minimum y]