I am trying to figure out how exactly does treesort from here work (I understand flatten, insert and foldr).
I suppose what’s being done in treesort is applying insert for each element on the list thus generating a tree and then flattening it. The only problem I can’t overcome here is where the list (that is the argument of the function) is hiding (because it is not written anywhere as an argument except for the function type declaration).
One more thing: since dot operator is function composition, why is it an error when I change: treesort = flatten . foldr insert Leaf to treesort = flatten( foldr insert Leaf )?
Exactly right.
In a functional language, you don’t have to give the arguments of a value of function type. For example if I write
I get a function of type
[[Char]] -> [Char]. Thisfunction expects an argument even though there’s no argument in the defining equation.
It’s exactly the same as if I had written
They don’t mean the same thing. What happens when each is applied to
x?You can see the applications associate differently, and
f. gis different fromf g.It’s a type error because
foldr insert Leafis a function from lists to trees, andflattenis meant to be applied to a single tree, not to a function.