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.  Why does sequence (map print [1, 2, 3, 4, 5]) produce [(),
      (), (), (), ()] at the end? (Olumide)
   2. Re:  Why does sequence (map print [1, 2, 3, 4, 5]) produce
      [(), (), (), (), ()] at the end? (David McBride)
   3.  foldr on infinite list to decide prime number (Chul-Woong Yang)
   4. Re:  foldr on infinite list to decide prime       number
      (Chul-Woong Yang)
   5. Re:  foldr on infinite list to decide prime       number
      (Francesco Ariis)
   6. Re:  foldr on infinite list to decide prime       number
      (Chul-Woong Yang)


----------------------------------------------------------------------

Message: 1
Date: Mon, 1 Feb 2016 17:28:27 +0000
From: Olumide <50...@web.de>
To: beginners@haskell.org
Subject: [Haskell-beginners] Why does sequence (map print [1, 2, 3, 4,
        5]) produce [(), (), (), (), ()] at the end?
Message-ID: <56af95bb.20...@web.de>
Content-Type: text/plain; charset=utf-8; format=flowed

Hello list,

The question says it all.

BTW, I'm studying LYH and I'm on the chapter on IO. The book offers and 
explanation but its not very clear -- to me at least.

Thanks,

- Olumide



------------------------------

Message: 2
Date: Mon, 1 Feb 2016 12:39:50 -0500
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Why does sequence (map print [1, 2,
        3, 4, 5]) produce [(), (), (), (), ()] at the end?
Message-ID:
        <can+tr40r6kgwia8tdda2untckvvgd9rp-mcvpzzrsorkiys...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

map :: (a -> b) -> [a] -> [b]
print :: Show a => a -> IO ()

map print :: Show a => [a] -> [IO ()]

print takes a showable a and creates a procedure that prints it out (IO ()).
So therefore map print causes each element in the array to become a
procedure that prints out its element, thus the return value is [IO()].

Note that it has not actually printed them out.  It merely has an array of
as yet unexecuted actions.  To print them you'd go like sequence (map
print) [1,2,3], or better yet, sequence_ which returns () instead of [()]

On Mon, Feb 1, 2016 at 12:28 PM, Olumide <50...@web.de> wrote:

> Hello list,
>
> The question says it all.
>
> BTW, I'm studying LYH and I'm on the chapter on IO. The book offers and
> explanation but its not very clear -- to me at least.
>
> Thanks,
>
> - Olumide
>
> _______________________________________________
> 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/20160201/90753424/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 2 Feb 2016 10:32:10 +0900
From: Chul-Woong Yang <cwy...@aranetworks.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] foldr on infinite list to decide prime
        number
Message-ID:
        <calmycjpezhgjjz_e1xkej3nxzgxihytqrv-u6dzh4e+syff...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi, all.

While I know that foldr can be used on infinite list to generate infinite
list,
I'm having difficulty in understaind following code:

isPrime n = n > 1 && -- from haskell wiki foldr (\p r -> p*p > n || ((n
`rem` p) /= 0 && r)) True primes primes = 2 : filter isPrime [3,5..]

primes is a infinite list of prime numbers, and isPrime does foldr to get a
boolean value.
What causes foldr to terminate folding?

Any helps will be deeply appreciated.

Thank you.

Chul-Woong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160202/7402c4e7/attachment-0001.html>

------------------------------

Message: 4
Date: Tue, 2 Feb 2016 10:36:03 +0900
From: Chul-Woong Yang <cwy...@aranetworks.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] foldr on infinite list to decide
        prime   number
Message-ID:
        <calmycjr-rt1ctx_+kksjztxbdlqrpfqz3q2+ag7423w63ld...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I feel sorry for posting mis-formatted code.
I re-post the question.
--

Hi, all.

While I know that foldr can be used on infinite list to generate infinite
list,
I'm having difficulty in understaind following code:

isPrime n = n > 1 &&  -- from haskell wiki
        foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r)) True primes
primes = 2 : filter isPrime [3,5..]

primes is a infinite list of prime numbers, and isPrime does foldr to get a
boolean value.
What causes foldr to terminate folding?

Any helps will be deeply appreciated.

Thank you.


2016-02-02 10:32 GMT+09:00 Chul-Woong Yang <cwy...@aranetworks.com>:

> Hi, all.
>
> While I know that foldr can be used on infinite list to generate infinite
> list,
> I'm having difficulty in understaind following code:
>
> isPrime n = n > 1 && -- from haskell wiki foldr (\p r -> p*p > n || ((n
> `rem` p) /= 0 && r)) True primes primes = 2 : filter isPrime [3,5..]
>
> primes is a infinite list of prime numbers, and isPrime does foldr to get
> a boolean value.
> What causes foldr to terminate folding?
>
> Any helps will be deeply appreciated.
>
> Thank you.
>
> Chul-Woong
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160202/1a7cba14/attachment-0001.html>

------------------------------

Message: 5
Date: Tue, 2 Feb 2016 03:01:23 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] foldr on infinite list to decide
        prime   number
Message-ID: <20160202020123.ga29...@casa.casa>
Content-Type: text/plain; charset=us-ascii

On Tue, Feb 02, 2016 at 10:32:10AM +0900, Chul-Woong Yang wrote:
> Hi, all.
> 
> While I know that foldr can be used on infinite list to generate infinite
> list,
> I'm having difficulty in understaind following code:
> 
> isPrime n = n > 1 && -- from haskell wiki foldr (\p r -> p*p > n || ((n
> `rem` p) /= 0 && r)) True primes primes = 2 : filter isPrime [3,5..]
> 
> primes is a infinite list of prime numbers, and isPrime does foldr to get a
> boolean value.
> What causes foldr to terminate folding?

foldr _immediately_ calls the passed function, hence /it can short
circuit/, that isn't the case for foldl.

I wrote an article to explain it [1]. It was drafted in a time when
foldr and friends were monomorphic (i.e. they only worked with lists),
but it should illustrate the point nicely.

Current polymorphic implementation of foldr is:

foldr :: (a -> b -> b) -> b -> t a -> b
foldr f z t = appEndo (foldMap (Endo #. f) t) z

and I must admit I have problems explaining why it terminates
early (as it does).

[1] http://ariis.it/static/articles/haskell-laziness/page.html (more
    complex cases section)


------------------------------

Message: 6
Date: Tue, 2 Feb 2016 11:12:35 +0900
From: Chul-Woong Yang <cwy...@aranetworks.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] foldr on infinite list to decide
        prime   number
Message-ID:
        <CALmycjowjev+dS3vt-fxCz06=mhy342yzaifoe33ectn_em...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Aha! I see.

Thank you for pointing the short-circuiting of (||),
and for nice article.

Regards,

2016-02-02 11:01 GMT+09:00 Francesco Ariis <fa...@ariis.it>:
> On Tue, Feb 02, 2016 at 10:32:10AM +0900, Chul-Woong Yang wrote:
>> Hi, all.
>>
>> While I know that foldr can be used on infinite list to generate infinite
>> list,
>> I'm having difficulty in understaind following code:
>>
>> isPrime n = n > 1 && -- from haskell wiki foldr (\p r -> p*p > n || ((n
>> `rem` p) /= 0 && r)) True primes primes = 2 : filter isPrime [3,5..]
>>
>> primes is a infinite list of prime numbers, and isPrime does foldr to get a
>> boolean value.
>> What causes foldr to terminate folding?
>
> foldr _immediately_ calls the passed function, hence /it can short
> circuit/, that isn't the case for foldl.
>
> I wrote an article to explain it [1]. It was drafted in a time when
> foldr and friends were monomorphic (i.e. they only worked with lists),
> but it should illustrate the point nicely.
>
> Current polymorphic implementation of foldr is:
>
> foldr :: (a -> b -> b) -> b -> t a -> b
> foldr f z t = appEndo (foldMap (Endo #. f) t) z
>
> and I must admit I have problems explaining why it terminates
> early (as it does).
>
> [1] http://ariis.it/static/articles/haskell-laziness/page.html (more
>     complex cases section)
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 92, Issue 2
****************************************

Reply via email to