【发布时间】:2018-03-26 23:06:23
【问题描述】:
import System.IO()
data Point = Point
{ pointX :: {-# UNPACK #-} !Double -- ^ X coordinate
, pointY :: {-# UNPACK #-} !Double -- ^ Y coordinate
} deriving (Show, Eq)
data Polygon = Polygon
{ points :: [Point]
, yvalue :: Int
} deriving (Show)
- 创建文件
test.hs - 将上面的两个自定义数据类型定义复制进去
- 打开ghci并输入
:l test.hs -
输入这个测试用例:
*test> let a = Polygon {points = [Point {pointX = 0.0, pointY = 0.0},Point {pointX = 4.0, pointY = 0.0},Point {pointX = 4.0, pointY = 2.0},Point {pointX = 4.0, pointY = 4.0},Point {pointX = 0.0, pointY = 4.0},Point {pointX = 0.0, pointY = 2.0},Point {pointX = 0.0, pointY = 0.0}], yvalue = 2} -
然后:
*test> let x = filter (<=(fromIntegral (yvalue a)).pointY) $ points a
您收到以下错误:
* couldn't match type `Point` with `Point -> c'
Expected type: [Point -> c]
Actual type: [Point]
* In the second argument of `($)', namely `points a'
In the expression:
filter (<=(fromIntegral (yvalue a)) . pointY) $ points a
In an equation for `x':
x = filter (<=(fromIntegral (yvalue a)).pointY) $ points a
* Relevant bindings include
x :: [Point -> c] (bound at <interactive>:92:5)
【问题讨论】:
-
你的意思是像
filter ((>= someThreshold) . pointY)? -
这让我更接近了。我添加了多边形结构。这将错误更改为:无法匹配类型
Point -> c0' withPoint' 预期类型:[Point] 实际类型:[Point -> c0] -
您可以将当前代码与您用于上下文的已知类型的标识符一起粘贴吗?
-
我认为编辑应该包括唯一缺少的东西。有一条不同的线返回周长。
-
这可能是一个简单的语法问题;没有minimal reproducible example 就很难确认。但你没有按我说的做:
let threshold = fromIntegral (yvalue x); ... filter ((>= threshold) . pointY)
标签: haskell filter custom-data-type