Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3331520
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:35:33+00:00 2026-05-17T23:35:33+00:00

I have a little problem understanding how to to the following in Haskell: Lets

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:35:33+00:00Added an answer on May 17, 2026 at 11:35 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem understanding XMLHttpRequest 's handlers. The specification says this about
This might be a stupid question, but I have a little problem with understanding
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
I have a little problem with the library 'Telerik'. Indeed I want to implement
I have a problem that comes from me following tutorials without really understanding what
I have a little problem: I have this in my model: validates :title, :presence
I have a little problem understanding the Java language public class PhonebookEntryList extends List<PhonebookEntry>
I have a little problem understanding how to create a child class's contructor, which
I have little problem with a replacement of a little part in an url.
I have little problem in regular expressin creation. Expected input: blahblahblah, blahblahblah, 'blahblahblah', blahblahblah,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.