<div dir="ltr"><div>This is the definition of list foldr</div><div><br></div><div><font face="monospace">foldr            :: (a -> b -> b) -> b -> [a] -> b<br>foldr _ z []     =  z<br>foldr f z (x:xs) =  f x (foldr f z xs)<br></font></div><div><br></div>In both foldl and foldr in the OP the n variable in lambda functions would seem to be for the accumulator, hence, I assume the n is considered the free variable? And then the wildcard  in each lambda function refers to the bound variable, i.e., the list argument's elements to be folded. So I can recreate<div><br></div><div><font face="monospace">foldr (+) 5 [1,2,3,4]</font></div><div><br></div><div>with</div><div><br></div><div><font face="monospace">foldr (\x n -> x + n) 5 [1,2,3,4]</font></div><div><br></div><div>They both return 15. The first one results in</div><div><br></div><div><font face="monospace">(+) 1 ((+) 2 ((+) 3 ((+) 4 5)))</font></div><div><br></div><div>but the second example I'm not sure how the (\x n -> x + n) is being applied in the form .<font face="monospace"> . . f x (foldr f z xs) </font><font face="arial, sans-serif">It obviously must be doing the same</font><font face="monospace"> (+) 1 ((+) 2 ((+) 3 ((+) 4 5)))</font> but how the function is being applied I don't understand.  Beta reduction doesn't get me very far</div><div><br></div><div><font face="monospace">\x n -> x + n (5)([1,2,3,4])</font></div><div><font face="monospace">\x 5 -> x + 5 ([1,2,3,4])</font></div><div><br></div><div>but obviously the enclosing lambda calc for <font face="monospace">foldr</font> is doing something to create the <font face="monospace">(+) 1 ((+) 2 ((+) 3 ((+) 4 5)))</font> form.</div><div><br></div><div>BTW, is the <font face="monospace">t a</font> format in <br></div><div><br></div><div><font face="monospace">:type foldr<br>foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b<br></font></div><div><font face="monospace"><br></font></div><div><font face="arial, sans-serif">something from category theory, i.e., for the list instance, </font><font face="monospace">t a</font><font face="arial, sans-serif"> is </font><font face="monospace">[a] </font><font face="arial, sans-serif">What is the algebraic syntax where </font><font face="monospace">t a</font><font face="arial, sans-serif"> becomes </font><font face="monospace">[a]</font><font face="arial, sans-serif"> in the case of lists? It would be nice to understand some day exactly what </font><font face="monospace">:i Foldable</font><font face="arial, sans-serif"> is saying </font></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Jan 16, 2021 at 4:36 PM Francesco Ariis <<a href="mailto:fa-ml@ariis.it">fa-ml@ariis.it</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Il 16 gennaio 2021 alle 16:10 Lawrence Bottorff ha scritto:<br>
> I have this<br>
> <br>
> myLength1 = foldl (\n _ -> n + 1) 0<br>
> <br>
> and this<br>
> <br>
> myLength2 = foldr (\_ n -> n + 1) 0<br>
> <br>
> I am guessing that foldl knows to assign the accumulator-seed argument to<br>
> the dependent variable and the list argument's elements recursively to the<br>
> independent variable; and with foldr to do the opposite. Is this a fair<br>
> assumption? BTW, where can I get a look at the code for fold functions; or<br>
> does the type definition answer my original question? Not really able to<br>
> decipher it so well<br>
> <br>
>  :t foldl<br>
> foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b<br>
<br>
foldl and foldr have slightly different signatures,<br>
<br>
    λ> :t +d foldl<br>
    foldl :: (b -> a -> b) -> b -> [a] -> b<br>
    λ> :t +d foldr<br>
    foldr :: (a -> b -> b) -> b -> [a] -> b<br>
<br>
(Notice  `b -> a -> b`  vs.  `a -> b -> b`), hence the lambdas have a<br>
different non-matched parameter.<br>
Does this answer your question? —F<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>