You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell任意数值类型自定义数据类型过滤:定义与测试指引

Implementing Filtering for Custom Numeric Data Types in Haskell

Let's walk through how to add filtering functionality to your custom Point and Polygon types, along with completing the steps you outlined.

Step 1: Complete Haskell Code with Filtering Functions

We'll extend your provided type definitions with reusable filtering functions that operate on the numeric fields of your custom types. Here's the full code:

import System.IO()

-- Point type with unpacked Double coordinates (optimized for performance)
data Point = Point { 
    pointX :: {-# UNPACK #-} !Double -- ^ X coordinate
  , pointY :: {-# UNPACK #-} !Double -- ^ Y coordinate 
} deriving (Show, Eq)

-- Polygon type containing a list of Points and an integer y-value marker
data Polygon = Polygon { 
    points :: [Point]
  , yvalue :: Int 
} deriving (Show)

-- Filter a list of Polygons to keep only those with yvalue greater than a threshold
filterPolygonsByY :: Int -> [Polygon] -> [Polygon]
filterPolygonsByY threshold = filter (\polygon -> yvalue polygon > threshold)

-- Filter the Points inside a single Polygon to keep only those with pointY above a threshold
filterPointsInPolygonByY :: Double -> Polygon -> Polygon
filterPointsInPolygonByY threshold polygon = 
    polygon { points = filter (\point -> pointY point > threshold) (points polygon) }

Step 2: Create the test.hs File

Copy the code above into a new file named test.hs on your system. You can use any text editor (like VS Code, Vim, or even Notepad) to do this.

Step 3: Load the File in GHCI

Open your terminal/command prompt and launch GHCI by typing ghci. Once GHCI is running, load your test.hs file with the command:

:l test.hs

You should see a message confirming the module was loaded successfully (e.g., [1 of 1] Compiling Main ( test.hs, interpreted )).

Step 4: Test the Filtering Functions with Sample Data

Let's create some test data and try out the filtering functions:

First, define a few Point instances:

*test> let bottomLeft = Point 0.0 0.0
*test> let bottomRight = Point 4.0 0.0
*test> let topRight = Point 4.0 3.0
*test> let topLeft = Point 0.0 3.0
*test> let midPoint = Point 2.0 2.0
*test> let highPoint = Point 1.0 5.0

Next, create some Polygon instances:

*test> let squarePoly = Polygon [bottomLeft, bottomRight, topRight, topLeft] 2
*test> let smallPoly = Polygon [midPoint, Point 3.0 1.0] 5
*test> let highPoly = Polygon [highPoint, Point 5.0 5.0] 1

Now test filtering Polygons by their yvalue:

-- Keep only Polygons where yvalue > 1
*test> filterPolygonsByY 1 [squarePoly, smallPoly, highPoly]
[Polygon {points = [Point {pointX = 0.0, pointY = 0.0},Point {pointX = 4.0, pointY = 0.0},Point {pointX = 4.0, pointY = 3.0},Point {pointX = 0.0, pointY = 3.0}], yvalue = 2},Polygon {points = [Point {pointX = 2.0, pointY = 2.0},Point {pointX = 3.0, pointY = 1.0}], yvalue = 5}]

Then test filtering Points inside a Polygon by their pointY:

-- Keep only Points in squarePoly where pointY > 0.0
*test> filterPointsInPolygonByY 0.0 squarePoly
Polygon {points = [Point {pointX = 4.0, pointY = 3.0},Point {pointX = 0.0, pointY = 3.0}], yvalue = 2}

These examples show how you can easily extend the pattern to filter on other numeric fields (like pointX or other custom numeric properties) by adjusting the lambda functions in the filter logic.

内容的提问来源于stack exchange,提问作者Eric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:08:33