获取树中定义的整数的和与积
我在理解如何在 Haskell 中执行以下操作时遇到了一些问题:
假设我有一个与此类似的语句:
- a * (b + c)
- a + (b * c)
- a + (b * ( c + d))
- a * (b + ( c * d))
- 等等。
我想在树中表达这些语句并评估每个语句的结果,对于初学者,我定义了以下数据结构:
data statement = Number Int
| Product statement statement
| Sum statement statement
deriving (Eq, Show)
对于要使用的示例树,我使用以下函数:
a :: statement
a = Product (Number 2) (Sum (Number 5) (Number 1))
现在我想构建一个函数 treeResult ,它给出定义的语句的结果,但我不知道如何解决这个问题。上述语句返回的整数应该是 12。
我的第一个猜测是编写一个函数,将“statement”作为参数并返回一个 int,对于仅使用简单语句的初学者来说。
treeResult :: statement -> Int
treeResult (Number a) = a
treeResult (Product (Number a) (Number b)) = a*b
treeResult (Sum (Number a) (Number b)) = a+b
现在我知道我需要一些可以递归工作的东西,但我不知道如何用 haskell 编写它,有人可以帮我吗?
I have a little problem understanding how to to the following in Haskell:
Lets say I have a statement similar to this ones:
- a * (b + c)
- a + (b * c)
- a + (b * ( c + d))
- a * (b + ( c * d))
- etc. etc.
I want to express these statements in a tree and evaluate the result of each, for starters I defined the following data structure:
data statement = Number Int
| Product statement statement
| Sum statement statement
deriving (Eq, Show)
For an example Tree to work with, I used the following function:
a :: statement
a = Product (Number 2) (Sum (Number 5) (Number 1))
Now I want to build a function treeResult which gives me the result of my statement defined, but I don't know how to approach this problem. The integer returned for the above statement should be 12.
My first guess was to write a function that takes "statement" as a parameter and returns an int, for starters only with simple statements.
treeResult :: statement -> Int
treeResult (Number a) = a
treeResult (Product (Number a) (Number b)) = a*b
treeResult (Sum (Number a) (Number b)) = a+b
Now I know that I need something that works recursive but I don't know how to write it in haskell, can someone help me out here, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:
statement
需要大写,因为它不是类型变量。但这是最不重要的事情。到目前为止显然是正确的。我们匹配
Number
构造函数,提取Int
并返回它 - 类型良好并执行其应有的操作。这是类型良好的,但不够通用。在此模式中,您将
Product
和Sum
的字段限制为Number
,但实际上它们可以是任何Statement< /代码>。因此,您需要定义
Product
/Sum
:但我们不能只将两个
Statement
相加/相乘。(+)
和(*)
没有为它们定义(我们现在正在这样做)。当一个操作数是Product
时,我们如何获取它的Int
值?通过评估它。Statement
是递归的,因此我们需要递归来计算它。 因此,该函数变为First off:
statement
needs to be capitalized since it's not a type variable. But that's the least important thing.Obviously correct so far. We're matching the
Number
constructor, extract theInt
and return that - well-typed and does what it should.This is well-typed, but it is not general enough. In this pattern, you are restricting the fields of
Product
andSum
toNumber
s, but in fact they can be anyStatement
. So you instead need to define theProduct
/Sum
:But we can't just add/multiply the two
Statements
.(+)
and(*)
are not defined for them (we're kind of doing that right now). When one operand is aProduct
, how do we get its value as anInt
? By evaluating it.Statement
is recursive, so we need recursion to evaluate it. Thus, the function becomes产品类似。这只是简单地识别
(顺便说一句,数据类型应该称为
Statement
并带有大写的 S。)
Similar for Product. This is simply recognizing
(BTW, the data type should be called
Statement
with an uppercase S.)