Hi,
maybe a few hints (or seeing them) helps:
The task gives you one in talking about consecutive instructions and jackdk tried to go from there but maybe it helps to spell this out a bit more.
If you are asked to look at consecutive instructions you can do this with pattern-matching, as you are allowed to have deep/nested patterns like
fast (Num a (Num b moreFun)) = ...
here on the right side of the equation you have two consecutive numbers a
and b
in scope and more of the Fun
-tree you are asked to transform (moreFun
).
And you are told to add consecutive numbers so surely you want a+b
and if you think about it you need to repack this, so Num (a+b) moreFun
is probably the obvious first attempt.
Sadly this will miss that inside moreFun
you could have even more places where you can merge stuff. So maybe you learned enough about recursion (hint: if the data-structure is defined with self-reference it’s usually down to recursion) to notice that you should use recursion here.
But watch out: Yes inside moreFun
could be other places to merge but the node Num (a+b) moreFun
itself might be mergable again (if moreFun
starts with another Num c evenMoreFun
) - so think about where you need to use the recursive call here but don’t overthink it (you don’t need a deeper pattern match for example).
If you are able to figure this out it should be easy to extent to the Word
part and it should hopefully be more or less obvious what to do with the missing patterns (no nesting, or Finish
)