获取树中定义的整数的和与积

发布于 2024-09-30 18:04:03 字数 911 浏览 3 评论 0原文

我在理解如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-10-07 18:04:03

首先:statement 需要大写,因为它不是类型变量。但这是最不重要的事情。

treeResult :: Statement -> Int
treeResult (Number x) = x

到目前为止显然是正确的。我们匹配 Number 构造函数,提取 Int 并返回它 - 类型良好并执行其应有的操作。

treeResult (Product (Number a) (Number b)) = a*b
treeResult (Sum (Number a) (Number b)) = a+b

这是类型良好的,但不够通用。在此模式中,您将 ProductSum 的字段限制为 Number,但实际上它们可以是任何 Statement< /代码>。因此,您需要定义 Product/Sum

treeResult (Product a b) = ...
treeResult (Sum a b) = ...

但我们不能只将两个 Statement 相加/相乘。 (+)(*) 没有为它们定义(我们现在正在这样做)。当一个操作数是 Product 时,我们如何获取它的 Int 值?通过评估它。 Statement 是递归的,因此我们需要递归来计算它。 因此,该函数变为

treeResult (Product a b) = (treeResult a) * (treeResult b)
treeResult (Sum a b) = (treeResult a) + (treeResult b)

First off: statement needs to be capitalized since it's not a type variable. But that's the least important thing.

treeResult :: Statement -> Int
treeResult (Number x) = x

Obviously correct so far. We're matching the Number constructor, extract the Int and return that - well-typed and does what it should.

treeResult (Product (Number a) (Number b)) = a*b
treeResult (Sum (Number a) (Number b)) = a+b

This is well-typed, but it is not general enough. In this pattern, you are restricting the fields of Product and Sum to Numbers, but in fact they can be any Statement. So you instead need to define the Product/Sum:

treeResult (Product a b) = ...
treeResult (Sum a b) = ...

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 a Product, how do we get its value as an Int? By evaluating it. Statement is recursive, so we need recursion to evaluate it. Thus, the function becomes

treeResult (Product a b) = (treeResult a) * (treeResult b)
treeResult (Sum a b) = (treeResult a) + (treeResult b)
橘和柠 2024-10-07 18:04:03
treeResult (Sum statement1 statement2) = treeResult statement1 + treeResult statement2

产品类似。这只是简单地识别

  • result of (a + b) == result of a + result of b

(顺便说一句,数据类型应该称为 Statement 并带有大写的 S。

data Statement = Number Int | Product Statement Statement | Sum Statement Statement
                 deriving (Eq, Show)

treeResult (Sum statement1 statement2) = treeResult statement1 + treeResult statement2

Similar for Product. This is simply recognizing

  • result of (a + b) == result of a + result of b

(BTW, the data type should be called Statement with an uppercase S.

data Statement = Number Int | Product Statement Statement | Sum Statement Statement
                 deriving (Eq, Show)

)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文