Foldl

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Foldl

Alexander Chen
Hi,

I guess it's time to get acquainted with foldr and foldl.

prelude>xs = [1..5] ++ undefined
prelude> foldr const 0 xs
1

I assume it goes something like this:

( 1 `const`(2 `const`(3 `const`(4 `const`(5 `const`(undefined `const` 0))))))
                                                              ( 5 `const` undefined)
                                               (4 `const` 5)    
                                (3 `const` 4)
                ( 2 `const` 3)
(1 `const`2)
= 1

========================================================================


What i don't get is the opposite:

prelude> foldl const 0 xs
error


in my mind this should go like this: 
((((((0`const`1)`const` 2) `const` 3 )`const` 4)`const`  5) `const` undefined)
                    (0 `const`2)
                                      (0`const` 3)            
                                                      ( 0`const`4)
                                                                       (0`const` 5)
                                                                                      (0 `const` undefined )
= 0



I have been told that the main difference between foldl and foldr is that foldl needs to evaluate the whole spline before it continues. And i guess that has something to do with it. What I don't understand is WHY foldl need to do this and foldr doesn't.


thanks in advance!

best,

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Foldl

Tony Morris-4
foldl does a loop, foldr does constructor replacement

https://www.youtube.com/watch?v=GPwtT31zKRY

On 5/28/20 4:55 AM, Alexander Chen wrote:

> Hi,
>
> I guess it's time to get acquainted with foldr and foldl.
>
> *prelude>xs = [1..5] ++ undefined*
> *prelude> foldr const 0 xs*
> *1*
>
> I assume it goes something like this:
>
> ( 1 `const`(2 `const`(3 `const`(4 `const`(5 `const`(undefined `const`
> 0))))))
>                                                               (
> 5 `const` undefined)
>                                                (4 `const` 5)    
>                                 (3 `const` 4)
>                 ( 2 `const` 3)
> (1 `const`2)
> = 1
>
> ========================================================================
>
>
> What i don't get is the opposite:
>
> *prelude> foldl const 0 xs*
> *error*
>
>
> in my mind this should go like this: 
> ((((((0`const`1)`const` 2) `const` 3 )`const` 4)`const`  5)
> `const` undefined)
>                     (0 `const`2)
>                                       (0`const` 3)            
>                                                       ( 0`const`4)
>                                                                      
>  (0`const` 5)
>                                                                        
>               (0 `const` undefined )
> = 0
>
>
>
> I have been told that the main difference between foldl and foldr is
> that foldl needs to evaluate the whole spline before it continues. And i
> guess that has something to do with it. What I don't understand is WHY
> foldl need to do this and foldr doesn't.
>
>
> thanks in advance!
>
> best,
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Foldl

Alexander Chen


May 27, 2020 11:33:58 PM CEST Tony Morris <[hidden email]> wrote:">
foldl does a loop, foldr does constructor replacement

https://www.youtube.com/watch?v=GPwtT31zKRY

On 5/28/20 4:55 AM, Alexander Chen wrote:

> Hi,
>
> I guess it's time to get acquainted with foldr and foldl.
>
> *prelude>xs = [1..5] ++ undefined*
> *prelude> foldr const 0 xs*
> 1
>
> I assume it goes something like this:
>
> ( 1 `const`(2 `const`(3 `const`(4 `const`(5 `const`(undefined `const`
> 0))))))
>                                                               (
> 5 `const` undefined)
>                                                (4 `const` 5)    
>                                 (3 `const` 4)
>                 ( 2 `const` 3)
> (1 `const`2)
> = 1
>
> ========================================================================
>
>
> What i don't get is the opposite:
>
> *prelude> foldl const 0 xs*
> error
>
>
> in my mind this should go like this: 
> ((((((0`const`1)`const` 2) `const` 3 )`const` 4)`const`  5)
> `const` undefined)
>                     (0 `const`2)
>                                       (0`const` 3)            
>                                                       ( 0`const`4)
>                                                                      
>  (0`const` 5)
>                                                                        
>               (0 `const` undefined )
> = 0
>
>
>
> I have been told that the main difference between foldl and foldr is
> that foldl needs to evaluate the whole spline before it continues. And i
> guess that has something to do with it. What I don't understand is WHY
> foldl need to do this and foldr doesn't.
>
>
> thanks in advance!
>
> best,
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners