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?
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:
This data structure has three items, including a list of
Bools:This one has three different possibilities, and because
Exprappears on the right hand side, it’s a bit like a tree.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.
mapdoes it for lists, andfmapdoes it for other things.Let’s define a function that takes a
Bool -> Booland maps it over the first example:and another one that maps over the triple. This time we have to make it work on each of the
Bools in the list.Notice I used the standard function
mapwhich works like this:so it applies
fto eachxin 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
at the top of my file, which would let me write
and then I could do
which would give me
but
fmapis more flexible: I could also door
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!