Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: foldr on infinite list to decide prime number
(Francesco Ariis)
2. Re: foldr on infinite list to decide prime number (Thomas Koster)
3. Re: Why does sequence (map print [1, 2, 3, 4, 5]) produce
[(), (), (), (), ()] at the end? (Olumide)
4. Re: Why does sequence (map print [1, 2, 3, 4, 5]) produce
[(), (), (), (), ()] at the end? (Erlend Hamberg)
----------------------------------------------------------------------
Message: 1
Date: Wed, 3 Feb 2016 02:55:09 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] foldr on infinite list to decide
prime number
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Tue, Feb 02, 2016 at 05:52:08PM -0700, derek riemer wrote:
> Doesn't foldr have to "reach" to the far right of the list of infinite
> primes to get the last number since it consumes from the right?
foldl is /left/ associative, foldr /right/ associative.
?> foldl1 (-) [1..3]
-4
-- which is: ((1-2)-3)
?> foldr1 (-) [1..3]
2
-- which is (1-(2-3))
Haskell being non strict (i.e. "proceeding from the outside"), it will
immediately short circuit on foldr but have to build the whole list first
on foldl.
------------------------------
Message: 2
Date: Wed, 3 Feb 2016 12:59:09 +1100
From: Thomas Koster <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] foldr on infinite list to decide
prime number
Message-ID:
<cag1wh7da3qewiexp8hkk5tx0ejw-qrg4uc+8fx5uftrxnqc...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On 3 February 2016 at 11:52, derek riemer <[email protected]> wrote:
> Doesn't foldr have to "reach" to the far right of the list of infinite
> primes to get the last number since it consumes from the right?
foldr does not consume from the right. It is right-associative.
See these simulations:
http://foldr.com
http://foldl.com
Notice that the essential difference between foldr and foldl is the
placement of the parentheses in the result, which the simulations show
very well. Whether that expression "short-circuits" or not depends on
the operator you used with your fold. Operators that are lazy in their
second argument (at least some of the time), like "||" and ":", can
short-circuit when used with foldr.
A common mistake is to think the parentheses force an evaluation
order, like "inside-out" for example. This is not the case for lazy
languages like Haskell. This unfortunate confusion probably arises
because parentheses often do prescribe evaluation order in basic
arithmetic and strict languages like Java.
Hope this helps,
Thomas Koster
------------------------------
Message: 3
Date: Wed, 3 Feb 2016 10:35:17 +0000
From: Olumide <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Why does sequence (map print [1, 2,
3, 4, 5]) produce [(), (), (), (), ()] at the end?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
On 01/02/2016 17:39, David McBride wrote:
> Note that it has not actually printed them out.
I hope you don't mind me asking but why then does sequence (map print
[1,2,3] ) return the numbers 1, 2, 3 (albeit followed by [(),(),()]).
> It merely has an array of as yet unexecuted actions. To print them
> you'd go like sequence (map print) [1,2,3] ...
Erm ... sequence (map print) [1,2,3] returns an error.
- Olumide
------------------------------
Message: 4
Date: Wed, 3 Feb 2016 12:30:02 +0100
From: Erlend Hamberg <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Why does sequence (map print [1, 2,
3, 4, 5]) produce [(), (), (), (), ()] at the end?
Message-ID:
<CAF__t3=ngk87rcptjkq36izrzsfqa2amohrx1jojvyqrog9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On 3 February 2016 at 11:35, Olumide <[email protected]> wrote:
> Erm ... sequence (map print) [1,2,3] returns an error.
That's because it will be parsed as `(sequence (map print) [1,2,3]`.
If you use `sequence (map print [1,2,3])`, it should work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160203/754794de/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 92, Issue 5
****************************************