如何在列表推导中对元组执行函数?哈斯克尔

时间:2014-12-10 22:13:05

标签: list haskell tuples

我有一个名为image的类型,它包含一组元组,它们是坐标。我正在尝试制作列表理解,我可以打印出x或y坐标列表的最大值和最小值。

example :: Image -> (Int,Int)
example e = [ (j,k) | (x,y) <- e, j <- (maximum x), k <- (minimum y)] 

我想打印(max x,min x)作为首发。

我一直收到错误,有人可以告诉我这样做的正确方法。

编辑:由于答案,我已将第二行更改为:

example e = [ (j,k) | (x,y) <- e, let j = maximum x, let k = minimum y]

然而得到这个错误:

Couldn't match expected type ‘[a]’ with actual type ‘Int’
Relevant bindings include j :: a (bound at image.hs:69:39)
In the first argument of ‘maximum’, namely ‘x’
In the expression: maximum x

表示j和k

1 个答案:

答案 0 :(得分:2)

更新回答:

以下是ImagePoint的定义:

type Image = [Point]
type Point = (Int,Int)

应该如何编写example

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

原始答案:

使用let

example e = [ (j,k) | (x,y) <- e, let j = maximum x, let k = minimum y]
相关问题