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 5842345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:00:21+00:00 2026-05-22T12:00:21+00:00

For haskell practice I want to implement a game where students/pupils should learn some

  • 0

For haskell practice I want to implement a game where students/pupils should learn some algebra playfully.

As basic datatype I want to use a tree:

  • with nodes that have labels and algebraic operators stored.
  • with leaves that have labels and variables (type String) or numbers

Now I want to define something like

data Tree = Leaf {l :: Label, val :: Expression}
          | Node {l :: Label, f :: Fun, lBranch :: Tree, rBranch :: Tree}

data Fun = "one of [(+),(*),(-),(/),(^)]"

-- type Fun = Int -> Int 

would work

Next things I think about is to make a ‘equivalence’ of trees – as multiplication/addition is commutative and one can simplify additions to multiplication etc. the whole bunch of algebraic operations.
I also have to search through the tree – by label I think is best, is this a good approach.

Any ideas what tags/phrases to look for and how to solve the “data Fun”.

  • 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-22T12:00:21+00:00Added an answer on May 22, 2026 at 12:00 pm

    To expand a bit on Edward Z. Yang’s answer:

    The simplest way to define your operators here is probably as a data type, along with the types for atomic values in leaf nodes and the expression tree as a whole:

    data Fun = Add | Mul | Sub | Div | Exp deriving (Eq, Ord, Show)
    
    data Val a = Lit a | Var String deriving (Eq, Ord, Show)
    
    data ExprTree a = Node String Fun (ExprTree a) (ExprTree a)
                    | Leaf String (Val a)
        deriving (Eq, Ord, Show)
    

    You can then define ExprTree a as an instance of Num and whatnot:

    instance (Num a) => Num (ExprTree a) where
        (+) = Node "" Add
        (*) = Node "" Mul
        (-) = Node "" Sub
        negate = Node "" Sub 0
        fromInteger = Leaf "" . Lit
    

    …which allows creating unlabelled expressions in a very natural way:

    *Main> :t 2 + 2
    2 + 2 :: (Num t) => t
    *Main> 2 + 2 :: ExprTree Int
    Node "" Add (Leaf "" (Lit 2)) (Leaf "" (Lit 2))
    

    Also, note the deriving clauses above on the data definitions, particularly Ord; this tells the compiler to automatically create an ordering relation on values of that type. This lets you sort them consistently which means you can, for instance, define a canonical ordering on subexpressions so that when rearranging commutative operations you don’t get stuck in a loop. Given some canonical reductions and subexpressions in canonical order, in most cases you’ll then be able to use the automatic equality relation given by Eq to check for subexpression equivalence.

    Note that labels will affect the ordering and equality here. If that’s not desired, you’ll need to write your own definitions for Eq and Ord, much like the one I gave for Num.

    After that, you can write some traversal and reduction functions, to do things like apply operators, perform variable substitution, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Haskell wiki states that you should use Cabal as your build system. However,
Haskell is givinig me a headache today. I want to handle an exception. When
Flowcharting. This ancient old practice that's been in use for over 1000 years now,
I've recently caught the FP bug (trying to learn Haskell), and I've been really
For the last few months, I've been putting some serious effort into learning Haskell
Is it generally considered a bad practice to use non-exhaustive pattern machings in functional
I'm learning Haskell, and one of my practice functions was a simple recursive permute
I'm curious as to how often experienced Haskell programmers really use type inference in
Reading Real world Haskell i found some intresting question about data types: This pattern
Is it possible to implement a quicksort in Haskell (with RANDOM-PIVOT) that still has

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.