<div dir="auto">Hello!<div dir="auto"><br></div><div dir="auto">I'm learning Haskell and I found an interesting implementation of init using foldr. However I have difficulty understand how it works. </div><div dir="auto"><br></div><div dir="auto"><b>init' xs = foldr f (const []) xs id</b></div><div dir="auto"><b>    where f x g h = h $ g (x:)</b></div><div dir="auto"><br></div><div dir="auto">Consider I have a input of <b>[1,2,3]</b>, then is would become</div><div dir="auto"><br></div><div dir="auto"><b>f 1 (f 2 ( f 3 (const []))) id</b></div><div dir="auto"><br></div><div dir="auto">I substitute those parameters into f and the innermost one becomes <b>h $ (const []) (1:)</b>, which is simply <b>h []</b>. However when I want to reduce the expression further, I found it's hard to grasp. The next one becomes <b>f 2 (h [])</b> , which is</div><div dir="auto"><br></div><div dir="auto"><b>h $ (h []) (2:)</b></div><div dir="auto"><br></div><div dir="auto">if it works like that. This looks confusing to me. To match the type of <b>foldr</b>, h should be of type <b>[a] -> [a]</b> and <b>h []</b> would just be of type <b>[a]</b>, which isn't applicable to <b>(2:)</b>.</div><div dir="auto"><br></div><div dir="auto">I also thought it in another way that <b>f x g</b> returns a function of type <b>([a] -> [a]) -> [a],</b> this kinda makes sense considering applying <b>id</b> afterwards. But then I realized I still don't know what this <b>h</b> is doing here. It looks like <b>h</b> conveys <b>g (x:)</b> from last time into the next application.</div><div dir="auto">Did I miss something when I think about doing fold with function as accumulator?<br></div><div dir="auto"><br></div><div dir="auto">I'd really appreciate if anyone could help me with this.</div></div>