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: Generating Infinite List of Fibonacci Numbers
(Harald Bögeholz)
2. error writing FilePath to text file (Dennis Raddle)
3. Re: error writing FilePath to text file (Michael Snoyman)
4. Re: error writing FilePath to text file (Dennis Raddle)
5. Re: error writing FilePath to text file (Michael Snoyman)
----------------------------------------------------------------------
Message: 1
Date: Wed, 29 Jun 2016 08:27:53 +0200
From: Harald Bögeholz <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Generating Infinite List of Fibonacci
Numbers
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Am 29.06.16 um 05:39 schrieb Desonte Jolivet:
>
> Hi,
>
> Problem: I would like to generate an infinite list of Fibonacci numbers.
> Although the below code does not work.
>
> fibs :: [Integer]fibs = 0 : 1 : [ n | x <-[2..], let n = ((fibs !! x-1) +
> (fibs !! x-2))]
>
> Thought: I'm assuming that I'm ignorant on how ranges/generators work with a
> list comprehension, which is why this code is not working.
>
> ghci output:
> *Main> fibs !! 01*Main> fibs !! 12*Main> fibs !! 2
> When I try and access the third element in the list ghci simply stalls.
> Can someone please give me a little more insight on why my code is not
> working.
You need to insert parentheses:
Prelude> let fibs = 0 : 1 : [ n | x <-[2..], let n = ((fibs !! (x-1)) +
(fibs !! (x-2)))]
Prelude> take 50 fibs
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049]
Btw a probably more efficient way to generate this list is
Prelude> let fibs' = 0 : 1 : zipWith (+) fibs' (tail fibs')
Prelude> take 50 fibs'
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155,165580141,267914296,433494437,701408733,1134903170,1836311903,2971215073,4807526976,7778742049]
Cheers
Harald
--
Harald Bögeholz <[email protected]> (PGP key available from servers)
Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417
http://www.ct.de/
int f[9814],b,c=9814,g,i;long a=1e4,d,e,h;
main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;}
(Arndt/Haenel)
Affe Apfel Vergaser
/*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover
Registergericht: Amtsgericht Hannover HRA 26709
Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH
Registergericht: Amtsgericht Hannover, HRB 60405
Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/
------------------------------
Message: 2
Date: Wed, 29 Jun 2016 01:20:47 -0700
From: Dennis Raddle <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] error writing FilePath to text file
Message-ID:
<cakxlvop-25tgbh26k71uydbehtvtoe-ykjd9b2ezbz1qium...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hello, I wrote a program that reads filenames using System.Directory
"listDirectory", does some tests on them, and writes certain file names (as
read into a FilePath variable by listDirectory) to a text file, which I
later parse with Parsec. I am getting an error on writing to the text file:
commitBuffer: invalid argument (invalid character)
What can I do about this? If I am reading some kind of unusual characters
from the FilePath, I want to choose some way of writing them so that the
resulting file can still be parsed by Text.Parsec.ByteString. I hope that I
can still rely on the "space" parser to find CR and LF, and don't want any
other surprises.
D
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160629/1e5179f7/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 29 Jun 2016 11:24:35 +0300
From: Michael Snoyman <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] error writing FilePath to text file
Message-ID:
<caka2jg+xapuhut08k2h0tygedndzb4ae13ekjnxu791xviz...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Jun 29, 2016 at 11:20 AM, Dennis Raddle <[email protected]>
wrote:
> Hello, I wrote a program that reads filenames using System.Directory
> "listDirectory", does some tests on them, and writes certain file names (as
> read into a FilePath variable by listDirectory) to a text file, which I
> later parse with Parsec. I am getting an error on writing to the text file:
>
> commitBuffer: invalid argument (invalid character)
>
> What can I do about this? If I am reading some kind of unusual characters
> from the FilePath, I want to choose some way of writing them so that the
> resulting file can still be parsed by Text.Parsec.ByteString. I hope that I
> can still rely on the "space" parser to find CR and LF, and don't want any
> other surprises.
>
> D
>
>
I'd recommend being explicit about the character encoding you're using, and
using the bytestring API for the I/O itself. As an example:
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.ByteString.Lazy as L
writeFileUtf8 :: FilePath -> String -> IO ()
writeFileUtf8 fp str = L.writeFile fp (TL.encodeUtf8 (TL.pack str))
The default handling of character encodings is reliant on environment
variables, which IME makes the textual file writing functions notoriously
fragile.
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160629/d489c006/attachment-0001.html>
------------------------------
Message: 4
Date: Wed, 29 Jun 2016 01:31:25 -0700
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] error writing FilePath to text file
Message-ID:
<CAKxLvoqM2jraN4sDGm+SSznGrXr3VqJ1fwNbkcY+q+R4F=e...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks. I don't know if I was clear that I'm writing the characters of the
FilePath to a file. I could use something like:
writeFilePaths :: FilePath -> [FilePath] -> IO ()
writeFilePaths fileToWrite fps = ...
The ByteStrings are for reading the files generated by the above and
parsing with Text.Parsec.ByteString, for efficiency (these are many
megabytes of text)
D
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160629/2b6b5aa9/attachment-0001.html>
------------------------------
Message: 5
Date: Wed, 29 Jun 2016 11:34:39 +0300
From: Michael Snoyman <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] error writing FilePath to text file
Message-ID:
<caka2jgk265iofpvbizc+mq1qp6dmicazkzvs2q2wt_s0y_v...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Jun 29, 2016 at 11:31 AM, Dennis Raddle <[email protected]>
wrote:
> Thanks. I don't know if I was clear that I'm writing the characters of
> the FilePath to a file. I could use something like:
>
> writeFilePaths :: FilePath -> [FilePath] -> IO ()
> writeFilePaths fileToWrite fps = ...
>
> The ByteStrings are for reading the files generated by the above and
> parsing with Text.Parsec.ByteString, for efficiency (these are many
> megabytes of text)
>
> D
>
>
>
A FilePath is just a type synonym for a String, so the same caveats will
apply to both. I'm guessing that you're dealing with some file paths that
have non-ASCII characters in them, and you have some locale environment
variables set that do not support UTF8. You can try rerunning your program
as-is with:
export LANG=en_US.UTF-8
in the shell (assuming you're on a POSIX system, Windows will behave
differently).
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160629/fdf2556d/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 96, Issue 19
*****************************************