【发布时间】:2016-06-28 20:43:34
【问题描述】:
我正在尝试使用以下代码编写 Haskell 模块:
module RectangleMover where
data Rectangle = Rectangle { xCoordinate :: Int
, yCoordinate :: Int
, width :: Int
, height :: Int
} deriving (Show)
move :: Rectangle -> Int -> Int -> Rectangle
-- Edit 1
move rec x y =
let rec' = { xCoordinate + x
, yCoordinate + y
}
return rec
要创建一个矩形,我会输入:
let rec = Rectangle 10 10 20 30
但我现在的问题是如何实现一个“移动”这个矩形的函数?
在 C# 或 Java 中,调用会是这样的:rec.move(20,20);
但是这将如何用 Haskell 编写呢?
不幸的是,这是我第一次尝试使用函数式编程语言...
编辑 1: 我在函数中添加了代码,但在“xCoordinate + x”处仍然出现解析错误...
【问题讨论】:
-
move作为参数的两个Int是什么意思?沿 x 轴和 y 轴的偏移量?还有什么? -
是 x 轴和 y 轴的偏移量