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

  • Home
  • SEARCH
  • 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 8787933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:01:08+00:00 2026-06-13T22:01:08+00:00

I have a homework: 1) define a data structure TTT for the tree where

  • 0

I have a homework:

1) define a data structure TTT for the tree where each vertex has 0, 1 or 2 children and each tree leaf(vertex with 0 children and itself) contains a list of natural numbers;

2) create a function mm which has 2 arguments – function f(Integer->Integer) and TTT based tree x. In result it should give TTT based tree which is made from x using function f for each element in the list(referenced to the 1) definition);

Function f can have following representations(a, b or c):

a :: Integer -> Integer
a x = x * x

b :: Integer -> Integer
b x = x `mod` 9

c :: Integer -> Integer
c x = x * x * x

Can anybody help me with this?

  • 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-06-13T22:01:08+00:00Added an answer on June 13, 2026 at 10:01 pm

    Important Advice

    It would really be worth working through Learn You a Haskell for Great Good. It’s an excellent tutorial.

    Practice! Play! Extend this assignment, by changing the brief. Can you do it for Strings instead of Integers? Can you do a tree with three subtrees? Can you do one where the branches have data too? Can you make a tree that takes any data type? Find out about Functors. Why are they any good? Can you make a tree that represents a calculation, with branches for operations like + and leaves for numbers?

    The more you play, the more confident you’ll be. Be the guy in class that found out about something before it came up. Everyone will ask you for help, then you’ll learn even more when you’re asked to solve the tricky problems anyone in your group has.

    Question 1: Tree datatype

    Here are some hints.

    This binary tree has two subtrees or a Boolean leaf:

    data BTree = Leaf Bool | Branch BTree BTree 
      deriving (Eq,Show)
    

    This data structure has three items, including a list of Bools:

    data Triple = Triple Int String [Bool]
      deriving (Eq,Show)
    

    This one has three different possibilities, and because Expr appears on the right hand side, it’s a bit like a tree.

    data Expr = Var Char | Lam Char Expr | Let Char Expr Expr
      deriving (Eq,Show)
    

    Now you need one with three possibilities, where the leaf has a list of Integers, and the other two have one subtree, or two subtrees. Put the ideas in the examples together.

    Question 2: map a function over your tree

    We call this apply-a-function-everywhere-you-can “map”ping the function. map does it for lists, and fmap does it for other things.

    Let’s define a function that takes a Bool -> Bool and maps it over the first example:

    mapBTree :: (Bool -> Bool) -> BTree -> BTree
    mapBTree f (Leaf b) = Leaf (f b)
    mapBTree f (Branch b1 b2) = Branch (mapBTree f b1) (mapBTree f b2)
    

    and another one that maps over the triple. This time we have to make it work on each of the Bools in the list.

    mapBoolTriple :: (Bool -> Bool) -> Triple -> Triple
    mapBoolTriple f (Triple i xs bs) = Triple i xs (map f bs)
    

    Notice I used the standard function map which works like this:

    map :: (a -> b) -> [a] -> [b]
    map f [] = []
    map f (x:xs) = f x : map f xs
    

    so it applies f to each x in my list, starting at the front.

    How I’d really do this kind of thing

    This isn’t how I’d do this for real, though. I’d add

    {-# LANGUAGE DeriveFunctor #-}
    

    at the top of my file, which would let me write

    data BinTree a = ALeaf a | ABranch (BinTree a) (BinTree a)
       deriving (Eq, Show, Functor)
    

    and then I could do

    fmap (*100) (ABranch (ALeaf 12) (ALeaf 34))
    

    which would give me

    ABranch (ALeaf 1200) (ALeaf 3400)
    

    but fmap is more flexible: I could also do

    fmap (<20) (ABranch (ALeaf 12) (ALeaf 34))
      -- ABranch (ALeaf True) (ALeaf False)
    

    or

    fmap show (ABranch (ALeaf 12) (ALeaf 34))
     -- ABranch (ALeaf "12") (ALeaf "34")
    

    without me writing a single line of the function fmap. I think that would give you 10/10 for using additional language features but 0/10 for solving the problem as set, so don’t do that, but keep it in mind and use it when you can.

    More advice

    Have fun learning Haskell. It can be mind-blowing at times, but it rewards you strongly for learning. You’ll be able to write some programs in Haskell a tenth of the length of programs in more conventional languages. Think more! Write less!

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

Sidebar

Related Questions

I have this homework question: Assume a Database table has 0 records at time
In my homework I have to define the logic operators as follows: Using this
I'm using Notepad++ and WinGHCi to do some homework and I have to define
For my homework assignment, I have to create a linked list and it must
I have a homework that include a new operation (a [n] b), given: a
I have a homework in which i have 640x480 image and i want to
I have a homework problem here I've been working on; here is the description:
I have a homework task to prove whether or not a particular variation on
I have a homework assignment where I need to take input from a file
I have a homework question that says: Problem 1: Given the array [ 22

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.