Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Understanding functions like f a b c = c $ b a (Austin Zhu) 2. Re: Understanding functions like f a b c = c $ b a (Bob Ippolito) 3. Re: Understanding functions like f a b c = c $ b a (Francesco Ariis) 4. Re: Understanding functions like f a b c = c $ b a (Francesco Ariis) ---------------------------------------------------------------------- Message: 1 Date: Fri, 7 Aug 2020 23:11:43 +0900 From: Austin Zhu <austinzhu...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Understanding functions like f a b c = c $ b a Message-ID: <CAM2mjt_y2=rt_gz3vwh+e1xb+a1us+koakvhs1tskrpm3bx...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello! I'm learning Haskell and I found an interesting implementation of init using foldr. However I have difficulty understand how it works. *init' xs = foldr f (const []) xs id* * where f x g h = h $ g (x:)* Consider I have a input of *[1,2,3]*, then is would become *f 1 (f 2 ( f 3 (const []))) id* I substitute those parameters into f and the innermost one becomes *h $ (const []) (1:)*, which is simply *h []*. However when I want to reduce the expression further, I found it's hard to grasp. The next one becomes *f 2 (h [])* , which is *h $ (h []) (2:)* if it works like that. This looks confusing to me. To match the type of *foldr*, h should be of type *[a] -> [a]* and *h []* would just be of type *[a]*, which isn't applicable to *(2:)*. I also thought it in another way that *f x g* returns a function of type *([a] -> [a]) -> [a],* this kinda makes sense considering applying *id* afterwards. But then I realized I still don't know what this *h* is doing here. It looks like *h* conveys *g (x:)* from last time into the next application. Did I miss something when I think about doing fold with function as accumulator? I'd really appreciate if anyone could help me with this. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200807/44735383/attachment-0001.html> ------------------------------ Message: 2 Date: Fri, 7 Aug 2020 22:24:57 -0700 From: Bob Ippolito <b...@redivi.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Understanding functions like f a b c = c $ b a Message-ID: <cacwmpm98kn_p4gimdmpgwpy-jpqshyneu8tgvtk6989vxch...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I think the part that is confusing is that there are two steps here, there is the *foldr*, and then there is the application of *id* to the result of the *foldr*. *foldr* is of type *(a -> b -> b) -> b -> [a] -> b*, and in your example the type for *a* is *Integer* (probably not precisely Integer, but let's just say it is for simplicity) and the type for *b* is *[Integer] -> [Integer]*. It would be better to think of it as *(foldr f (const []) xs) id*. Another way to think of it is that *foldr* replaces the list *:* constructor with the function (*f*) and the *[]* constructor with the given *b* (*id*). Here's how I would think about the computation. In Haskell it's usually best to start with the outside and work in, due to the non-strict evaluation. At the end I've removed the bold from the terms that are already completely reduced. *init' [1, 2, 3]* *(foldr f (const []) (1 : 2 : 3 : [])) id* *(1 `f` (2 `f` (3 `f` const []))) id* *id ((2 `f` (3 `f` const [])) (1:))* *(2 `f` (3 `f` const [])) (1:)* 1 :* ((3 `f` const []) (2:))* 1 : 2 :* (const [] (3:))* 1 : 2 : [] On Fri, Aug 7, 2020 at 7:12 AM Austin Zhu <austinzhu...@gmail.com> wrote: > Hello! > > I'm learning Haskell and I found an interesting implementation of init > using foldr. However I have difficulty understand how it works. > > *init' xs = foldr f (const []) xs id* > * where f x g h = h $ g (x:)* > > Consider I have a input of *[1,2,3]*, then is would become > > *f 1 (f 2 ( f 3 (const []))) id* > > I substitute those parameters into f and the innermost one becomes *h $ > (const []) (1:)*, which is simply *h []*. However when I want to reduce > the expression further, I found it's hard to grasp. The next one becomes *f > 2 (h [])* , which is > > *h $ (h []) (2:)* > > if it works like that. This looks confusing to me. To match the type of > *foldr*, h should be of type *[a] -> [a]* and *h []* would just be of > type *[a]*, which isn't applicable to *(2:)*. > > I also thought it in another way that *f x g* returns a function of type *([a] > -> [a]) -> [a],* this kinda makes sense considering applying *id* > afterwards. But then I realized I still don't know what this *h* is doing > here. It looks like *h* conveys *g (x:)* from last time into the next > application. > Did I miss something when I think about doing fold with function as > accumulator? > > I'd really appreciate if anyone could help me with this. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20200807/fb6da7bf/attachment-0001.html> ------------------------------ Message: 3 Date: Sat, 8 Aug 2020 13:24:37 +0200 From: Francesco Ariis <fa...@ariis.it> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Understanding functions like f a b c = c $ b a Message-ID: <20200808112437.GA15774@extensa> Content-Type: text/plain; charset=us-ascii +1 Il 07 agosto 2020 alle 22:24 Bob Ippolito ha scritto: > I think the part that is confusing is that there are two steps here, there > is the *foldr*, and then there is the application of *id* to the result of > the *foldr*. *foldr* is of type *(a -> b -> b) -> b -> [a] -> b*, and in > your example the type for *a* is *Integer* (probably not precisely Integer, > but let's just say it is for simplicity) and the type for *b* is *[Integer] > -> [Integer]*. It would be better to think of it as *(foldr f (const []) > xs) id*. Another way to think of it is that *foldr* replaces the list *:* > constructor with the function (*f*) and the *[]* constructor with the given > *b* (*id*). Here's how I would think about the computation. In Haskell it's > usually best to start with the outside and work in, due to the non-strict > evaluation. At the end I've removed the bold from the terms that are > already completely reduced. > > *init' [1, 2, 3]* > *(foldr f (const []) (1 : 2 : 3 : [])) id* > *(1 `f` (2 `f` (3 `f` const []))) id* > *id ((2 `f` (3 `f` const [])) (1:))* > > *(2 `f` (3 `f` const [])) (1:)* > 1 :* ((3 `f` const []) (2:))* > 1 : 2 :* (const [] (3:))* > 1 : 2 : [] > > > On Fri, Aug 7, 2020 at 7:12 AM Austin Zhu <austinzhu...@gmail.com> wrote: > > > Hello! > > > > I'm learning Haskell and I found an interesting implementation of init > > using foldr. However I have difficulty understand how it works. > > > > *init' xs = foldr f (const []) xs id* > > * where f x g h = h $ g (x:)* > > > > Consider I have a input of *[1,2,3]*, then is would become > > > > *f 1 (f 2 ( f 3 (const []))) id* > > > > I substitute those parameters into f and the innermost one becomes *h $ > > (const []) (1:)*, which is simply *h []*. However when I want to reduce > > the expression further, I found it's hard to grasp. The next one becomes *f > > 2 (h [])* , which is > > > > *h $ (h []) (2:)* > > > > if it works like that. This looks confusing to me. To match the type of > > *foldr*, h should be of type *[a] -> [a]* and *h []* would just be of > > type *[a]*, which isn't applicable to *(2:)*. > > > > I also thought it in another way that *f x g* returns a function of type > > *([a] > > -> [a]) -> [a],* this kinda makes sense considering applying *id* > > afterwards. But then I realized I still don't know what this *h* is doing > > here. It looks like *h* conveys *g (x:)* from last time into the next > > application. > > Did I miss something when I think about doing fold with function as > > accumulator? > > > > I'd really appreciate if anyone could help me with this. > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 4 Date: Sat, 8 Aug 2020 13:37:42 +0200 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Understanding functions like f a b c = c $ b a Message-ID: <20200808113742.GA22015@extensa> Content-Type: text/plain; charset=utf-8 Il 08 agosto 2020 alle 13:24 Francesco Ariis ha scritto: > +1 This is silly me thinking I was replying to a Discourse thread liking the post… ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 146, Issue 1 *****************************************