Re: [Haskell-cafe] generalized, tail-recursive left fold that can

2013-02-21 Thread Roman Cheplyaka
Thanks, I see now where my mistake was. Laziness (or call by name) is needed to make the step from (\e a z -> a (f z e)) (head l) (foldr (\e a z -> a (f z e)) id (tail l) z) (f z (head l)) to \z -> foldr (\e a z -> a (f z e)) id (tail l) (f z (head l)) without evaluating foldr

Re: [Haskell-cafe] generalized, tail-recursive left fold that can

2013-02-19 Thread oleg
> > That said, to express foldl via foldr, we need a higher-order > > fold. There are various problems with higher-order folds, related to > > the cost of building closures. The problems are especially severe > > in strict languages or strict contexts. Indeed, > > > > foldl_via_foldr f z l = fol

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Roman Cheplyaka
* o...@okmij.org [2013-02-19 06:27:10-] > > As others have pointed out, _in principle_, foldr is not at all > deficient. We can, for example, express foldl via foldr. Moreover, we > can express head, tail, take, drop and even zipWith through > foldr. That is, the entire list processing librar

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread oleg
As others have pointed out, _in principle_, foldr is not at all deficient. We can, for example, express foldl via foldr. Moreover, we can express head, tail, take, drop and even zipWith through foldr. That is, the entire list processing library can be written in terms of foldr: http://okm

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Niklas Hambüchen
On 18/02/13 16:10, Petr Pudlák wrote: > - `foldr` is unsuitable because it counts the elements from the end, > while `!!` needs counting from the start (and it's not tail recursive). It is common misconception that foldr processes the list "from the right". foldr "brackets" from the right, but th

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Petr Pudlák
Thanks Roman and Andres for the tip. I knew the trick with accumulating a function, but I had never imagined it could work so efficiently. I thought the problem with using foldr would be that the function would be neither tail recursive nor efficient and so I hadn't even tried. Apparently that was

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Roman Cheplyaka
* Roman Cheplyaka [2013-02-18 18:28:47+0200] > * Petr Pudlák [2013-02-18 17:10:26+0100] > > Dear Haskellers, > > > > while playing with folds and trying to implement `!!` by folding, I came to > > the conclusion that: > > > > - `foldr` is unsuitable because it counts the elements from the end,

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Roman Cheplyaka
* Petr Pudlák [2013-02-18 17:10:26+0100] > Dear Haskellers, > > while playing with folds and trying to implement `!!` by folding, I came to > the conclusion that: > > - `foldr` is unsuitable because it counts the elements from the end, while > `!!` needs counting from the start (and it's not tai

Re: [Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Andres Löh
Hi. > while playing with folds and trying to implement `!!` by folding, I came to > the conclusion that: > > - `foldr` is unsuitable because it counts the elements from the end, while > `!!` needs counting from the start (and it's not tail recursive). What is the problem with the following defini

[Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Petr Pudlák
Dear Haskellers, while playing with folds and trying to implement `!!` by folding, I came to the conclusion that: - `foldr` is unsuitable because it counts the elements from the end, while `!!` needs counting from the start (and it's not tail recursive). - `foldl` is also unsuitable, because it a

[Haskell-cafe] generalized, tail-recursive left fold that can finish tne computation prematurely

2013-02-18 Thread Petr Pudlák
Dear Haskellers, while playing with folds and trying to implement `!!` by folding, I came to the conclusion that: - `foldr` is unsuitable because it counts the elements from the end, while `!!` needs counting from the start (and it's not tail recursive). - `foldl` is also unsuitable, because it a