How to create function to short down the result

Haskell, I need to modify it so it will be quicker.

data Fun =
Word String Fun
| Num Int Fun
| Finish
deriving Show
I need this function fast :: Fun → Fun that concatenates any consecutive Num instructions by adding their times together, and concatenates any consecutive Word commands by adding an extra space between them.

fast (Word “One” (Word “Fine it” (Num 1 (Num 2 (Num 1 Finish)))))

result of above in a short way Word “One Fine it” (Num 4 Finish)

So I have this
fast :: Fun → Fun
fast Finish =
fast (Word k l) =
fast (Num g h) =

Meta: Please

  • Use a single thread for each additional question relating to your problem so that it doesn’t take over the whole front page.
  • Use code block formatting around your code to make it easier to read. This is the </> button or you can put three backticks (``` - on most keyboards, the key to the left of [1]) before and after your code.

As for your problem: I find it helpful to write out several examples using input values, as these can suggest patterns to write when you write out the actual definition. For example: what will these evaluations produce?

fast Finish = ?
fast (Num 1 Finish) = ?
fast (Num 2 (Num 1 Finish)) = ?
fast (Num 1 (Num 2 (Num 1 Finish))) = ?
fast (Word "Fine it" (Num 1 (Num 2 (Num 1 Finish)))) = ?
fast (Word "One" (Word "Fine it" (Num 1 (Num 2 (Num 1 Finish))))) = ?

Can you write any of these in terms of any of the others?


fast 0
fast (Num 1 )
fast (Num 3)
fast Word “Fine it” (Num 4)
something like this all of them. Like I am not getting how I can write it to achieve the RESULT

“Something like this” is not going to be enough to solve the problem. You must think precisely, and part of that is getting the brackets and the types right. None of your examples type check, so if you need assistance working out exactly what the return values of these examples would be, try playing around in GHCi and build them up in there. Then copy the outputs back into this post.



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)