Fan*_*oru 2 constructor haskell types
我有这个代码
type Point = (Int, Int)
data Points
= Rectangle Point Point
| Union Points Points
| Difference Points Points
inMax :: Int -> Int -> Int
inMax a b
| a < b = b
| otherwise = a
inMin :: Int -> Int -> Int
inMin a b
| a < b = a
| otherwise = b
inPoints :: Point -> Points -> Bool
inPoints (x, y) Rectangle (x1, y1) (x2, y2) = ((inMin x1 x2) <= x && x <= (inMax x1 x2)) && ((inMin y1 y2) <= y && y <= (inMax y1 y2))
Run Code Online (Sandbox Code Playgroud)
我想知道一个点是否在 Rectangle 中,但我得到了下一个错误:构造函数 `Rectangle' 应该有 2 个参数,但没有给出任何参数。为什么?我应该怎么修?
为了“解包” Rectangle,然后Rectange在括号之间写:
inPoints :: Point -> Points -> Bool
inPoints (x, y) (Rectangle (x1, y1) (x2, y2)) = --- …
-- ↑ parenthesis ↑Run Code Online (Sandbox Code Playgroud)
因此,我们执行模式匹配以获得 的点Rectangle。你不能使用Rectange它自己作为一个模式。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |