Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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: Keyboard input (Daniel Fischer)
2. text file to data type (Brent Pedersen)
3. Re: text file to data type (Joe Fredette)
4. Re: text file to data type (Heinrich Apfelmus)
5. Re: Re: text file to data type (Patrick LeBoutillier)
6. Parallel mutation (Jon Harrop)
7. Re: Parallel mutation (Daniel Fischer)
8. recursively building lists (Torsten Otto)
9. Re: recursively building lists (Daniel Fischer)
10. Re: recursively building lists (Edward Z. Yang)
----------------------------------------------------------------------
Message: 1
Date: Mon, 7 Dec 2009 23:36:10 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Keyboard input
To: legajid <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 07 Dezember 2009 22:52:42 schrieb legajid:
> Hello,
> ok for the responses.
> However, about the strange behaviour of getChar, i forgot to say i'm
> running on Windows XP with GHci 6.10.4
Voilà, c'est ton problème ;)
Somehow, buffering (or rather not buffering) doesn't work properly on Windows.
Ordinarily,
in ghci, stdin is unbuffered, meaning each character you type is immediately
passed to the
application (for binaries, you'd have to "hSetBuffering stdin NoBuffering"),
that seems
not to be the case on Windows (http://hackage.haskell.org/trac/ghc/ticket/2189).
> It seems that getChar buffers all characters, including newline; so,
> typing A + enter is equivalent to typing 2 characters successively.
> To successfully solve the problem, i did the following :
>
> guessLetter :: IO Char
> guessLetter = do
> putStrLn "Guess a letter (9 to end): "
> cl <- getLine
> let c= extr cl
> -- c <- getChar
> -- putChar '\b'
> return (toUpper c)
>
> extr :: String -> Char
> extr (c:cs)=c
>
>
> Notice that, on my system, putChar has no visible effect.
Buffering again. Just to test what works, you can try
a)
ghci> :m +System.IO
ghci> hSetBuffering stdout NoBuffering
ghci> putChar 'A'
(turning off buffering for stdout *may* work, even if it doesn't for stdin)
if the next line you see is
Aghci>
it worked
b) explicitly flush stdout
do ...
putChar 'X'
hFlush stdout
>
> Seeking for info, i found this mail :
> http://old.nabble.com/How-to-getCh-on-MS-Windows-command-line--td20414545.h
>tml that talks about a similar issue on Windows
>
> Didier.
>
------------------------------
Message: 2
Date: Mon, 7 Dec 2009 19:43:04 -0800
From: Brent Pedersen <[email protected]>
Subject: [Haskell-beginners] text file to data type
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
hi, i have files with lots (millions of rows) of data like this:
chr6 chr10 96.96 3392 101 2 79030508 79033899 4160024
4163413 0.0 5894
chr6 chr10 93.19 4098 228 13 117152751 117156826
11355389 11359457 0.0 5886
chr6 chr10 95.82 3445 130 5 112422073 112425513 7785396
7788830 0.0 5666
and i'd like to read it into a type like this:
data Blast = Blast { query :: S.ByteString
, subject :: S.ByteString
, hitlen :: Int
, mismatch :: Int
, gaps :: Int
, qstart :: Int
, qstop :: Int
, sstart :: Int
, sstop :: Int
, pctid :: Double
, evalue :: Double
, bitscore :: Double
} deriving (Show)
where each of those fields corresponds to a column in the file.
in python, i do something like:
>>> line = [fn(col) for fn, col in zip([str, str, int, int, int, int, int, int,
>>> int, float, float], sline.split("\t")]
what's a fast, simple way to do this in haskell?
is it something like:
instance Read Blast where
readsPrec s = ?????
any pointers on where to look for simple examples of this type of parsing would
be much appreciated.
thanks,
-brentp
------------------------------
Message: 3
Date: Mon, 7 Dec 2009 22:52:47 -0500
From: Joe Fredette <[email protected]>
Subject: Re: [Haskell-beginners] text file to data type
To: Brent Pedersen <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
My suggestion would be to look into writing a parser (via parsec) to
handle this. Parsec is fairly easy to learn, and since your data is a
pretty simple format, the parser won't be hard to write. Parsec will
then give you a parser which you can run on the file, it'll catch
parse errors, it's all around very lovely to use. There is a chapter
of Real World Haskell on the subject, and I'm sure we'll be happy to
help with whatever isn't covered.
/Joe
On Dec 7, 2009, at 10:43 PM, Brent Pedersen wrote:
> hi, i have files with lots (millions of rows) of data like this:
> chr6 chr10 96.96 3392 101 2 79030508 79033899 4160024
> 4163413 0.0 5894
> chr6 chr10 93.19 4098 228 13 117152751 117156826
> 11355389 11359457 0.0 5886
> chr6 chr10 95.82 3445 130 5 112422073 112425513 7785396
> 7788830 0.0 5666
>
> and i'd like to read it into a type like this:
>
> data Blast = Blast { query :: S.ByteString
> , subject :: S.ByteString
> , hitlen :: Int
> , mismatch :: Int
> , gaps :: Int
> , qstart :: Int
> , qstop :: Int
> , sstart :: Int
> , sstop :: Int
> , pctid :: Double
> , evalue :: Double
> , bitscore :: Double
> } deriving (Show)
>
> where each of those fields corresponds to a column in the file.
> in python, i do something like:
>
>>>> line = [fn(col) for fn, col in zip([str, str, int, int, int, int,
>>>> int, int, int, float, float], sline.split("\t")]
>
> what's a fast, simple way to do this in haskell?
>
> is it something like:
>
> instance Read Blast where
> readsPrec s = ?????
>
>
> any pointers on where to look for simple examples of this type of
> parsing would
> be much appreciated.
> thanks,
> -brentp
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Tue, 08 Dec 2009 11:29:39 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: text file to data type
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Joe Fredette wrote:
> My suggestion would be to look into writing a parser (via parsec) to
> handle this. Parsec is fairly easy to learn, and since your data is a
> pretty simple format, the parser won't be hard to write.
While I'm all for using a proper parser, Brent Pedersen notes that his
data will have millions of rows, so that Parsec is likely to run into
memory problems.
I think something along the lines of
import Data.ByteString.Lazy.Char8 as B
parse = map (zipWith ($) formats . B.split '\t') . B.lines
where
formats = [str, str, int, int, int, int, int,
int, int, float, float]
int = fst . fromJust . readInt
float = \s -> read (unpack s) :: Double
str = id
will do just fine. (The implementation of float is a kludge, I think
there's something on hackage for that, though?)
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 5
Date: Tue, 8 Dec 2009 09:38:43 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Re: text file to data type
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi all,
On Tue, Dec 8, 2009 at 5:29 AM, Heinrich Apfelmus
<[email protected]> wrote:
> Joe Fredette wrote:
>> My suggestion would be to look into writing a parser (via parsec) to
>> handle this. Parsec is fairly easy to learn, and since your data is a
>> pretty simple format, the parser won't be hard to write.
>
> While I'm all for using a proper parser, Brent Pedersen notes that his
> data will have millions of rows, so that Parsec is likely to run into
> memory problems.
>
> I think something along the lines of
>
> import Data.ByteString.Lazy.Char8 as B
>
> parse = map (zipWith ($) formats . B.split '\t') . B.lines
> where
> formats = [str, str, int, int, int, int, int,
> int, int, float, float]
> int = fst . fromJust . readInt
> float = \s -> read (unpack s) :: Double
> str = id
>
I've been looking at this example and I can't figure it out how it works.
Seems to me that "formats" is a list of functions that return different types.
How does this work?
Patrick
> will do just fine. (The implementation of float is a kludge, I think
> there's something on hackage for that, though?)
>
>
> Regards,
> Heinrich Apfelmus
>
> --
> http://apfelmus.nfshost.com
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 6
Date: Tue, 8 Dec 2009 20:56:45 +0000
From: Jon Harrop <[email protected]>
Subject: [Haskell-beginners] Parallel mutation
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Can you mutate different parts of the same array from different threads in
parallel using GHC? Someone once told me this was possible but I cannot see
how...
--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e
------------------------------
Message: 7
Date: Tue, 8 Dec 2009 22:37:41 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Parallel mutation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Dienstag 08 Dezember 2009 21:56:45 schrieb Jon Harrop:
> Can you mutate different parts of the same array from different threads in
> parallel using GHC? Someone once told me this was possible but I cannot see
> how...
You can (take a look at Control.Concurrent), but very probably ***You really
shouldn't***,
so I won't elaborate.
What would you want to do that for?
------------------------------
Message: 8
Date: Tue, 8 Dec 2009 23:34:30 +0100
From: Torsten Otto <[email protected]>
Subject: [Haskell-beginners] recursively building lists
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi!
While trying to implement "words", we ran into the question of how to build
lists of lists. The trouble boils down to this:
test = []:[]
is no problem, just as
Prelude> []:[]:[]:[]
[[],[],[]]
works fine. Now trying to put the two together _is_ a problem:
testlist 1 = []:[]
testlist n = []:(testlist n-1)
No instance for (Num [[a]])
arising from a use of `testlist' at <interactive>:1:0-9
Possible fix: add an instance declaration for (Num [[a]])
In the expression: testlist 2
In the definition of `it': it = testlist 2
Can someone please explain, what is going on here?
Regards,
Torsten
------------------------------
Message: 9
Date: Tue, 8 Dec 2009 23:43:07 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] recursively building lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Dienstag 08 Dezember 2009 23:34:30 schrieb Torsten Otto:
> Hi!
>
> While trying to implement "words", we ran into the question of how to build
> lists of lists. The trouble boils down to this:
>
> test = []:[]
>
> is no problem, just as
>
> Prelude> []:[]:[]:[]
> [[],[],[]]
>
> works fine. Now trying to put the two together _is_ a problem:
>
> testlist 1 = []:[]
> testlist n = []:(testlist n-1)
>
> No instance for (Num [[a]])
> arising from a use of `testlist' at <interactive>:1:0-9
> Possible fix: add an instance declaration for (Num [[a]])
> In the expression: testlist 2
> In the definition of `it': it = testlist 2
>
> Can someone please explain, what is going on here?
Yes. Function application has higher precedence than aritthmetic operations, so
the RHS of
testlist n is parsed as
[] : ( (testlist n) - 1)
So, by the equation for testlist 1, the compiler knows that
testlist :: (Num n) => n -> [[a]]
In the second equation, you try to subtract 1 from a value of type [[a]], that
means you
need a Num instance for that type.
What you want is
testlist n = []:testlist (n-1)
HTH,
Daniel
------------------------------
Message: 10
Date: Tue, 08 Dec 2009 17:45:19 -0500
From: "Edward Z. Yang" <[email protected]>
Subject: Re: [Haskell-beginners] recursively building lists
To: Torsten Otto <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1260312279-sup-7...@ezyang>
Content-Type: text/plain; charset=UTF-8
Excerpts from Torsten Otto's message of Tue Dec 08 17:34:30 -0500 2009:
> testlist 1 = []:[]
> testlist n = []:(testlist n-1)
Works for me on GHC 6.10.4. What compiler are you using?
Cheers,
Edward
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 6
****************************************