Send Beginners mailing list submissions to
        beginners@haskell.org

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
        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.  List Manipulation (Lorenzo Isella)
   2. Re:  List Manipulation (Daniel Fischer)
   3. Re:  specify type for a function in GHCi (Gaius Hammond)
   4.  Stack space overflow with foldl' (Ryan Prichard)
   5.  Network programming (select(2)) for Haskell?
      (Patrick LeBoutillier)
   6.  Re: Debugging overlapping instances (mtl vs      transformers)
      (Paolo Losi)
   7. Re:  Network programming (select(2)) for Haskell? (Johan Tibell)
   8.  Re: Network programming (select(2)) for Haskell? (Paolo Losi)
   9.  Cabal and .chs modules dependencies (John Obbele)


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

Message: 1
Date: Thu, 09 Sep 2010 19:12:21 +0200
From: Lorenzo Isella <lorenzo.ise...@gmail.com>
Subject: [Haskell-beginners] List Manipulation
To: beginners@haskell.org
Message-ID: <4c891575.6050...@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Dear All,
Thanks to the help I got on the mailing list, I was able to load a table 
of integers as a list where every entry is a list of integers.
To fix the ideas consider

main :: IO ()

main = do
    txt <- readFile "mydata.dat"

    let dat :: [[Integer]]

        dat = convert txt

    print dat -- this prints out my chunk of data



convert x = (map (map read . words) . lines) x


and the file mydata.txt is given by

1246191122 1336 1337
1246191142 1336 1337
1246191162 1336 1337
1246191182 1336 1337
1246191202 1336 1337
1246191222 1336 1337
1246191242 1336 1337
1246191262 1336 1337
1246191282 1336 1337
1246191302 1336 1337
1246191322 1336 1337
1246191342 1336 1337
1246191362 1336 1337
1246191382 1336 1337
1246191402 1336 1337
1246191422 1336 1337


Up to some extent, I am done (later on I found out that reading this 
into a matrix for hmatrix is a one-liner), but I would like to achieve 
more just performing standard list manipulations.
For instance, since my data files are always this simple (integers/real 
arrays of numbers)

(1) How can I tell the length of every sublist in my list?
(2) How can I e.g. stitch together the 1st element in every sublist in 
order to get the 1st column of the original data file?

Many thanks

Lorenzo


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

Message: 2
Date: Thu, 9 Sep 2010 20:46:05 +0200
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] List Manipulation
To: beginners@haskell.org
Cc: Lorenzo Isella <lorenzo.ise...@gmail.com>
Message-ID: <201009092046.06287.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

On Thursday 09 September 2010 19:12:21, Lorenzo Isella wrote:
>
> Up to some extent, I am done (later on I found out that reading this
> into a matrix for hmatrix is a one-liner), but I would like to achieve
> more just performing standard list manipulations.
> For instance, since my data files are always this simple (integers/real
> arrays of numbers)
>
> (1) How can I tell the length of every sublist in my list?

map length

gives the list of lengths

(if all sublists are known to have the same length, of course one length is 
enough, e.g. head . map length if there is known to be at least one 
sublist).

> (2) How can I e.g. stitch together the 1st element in every sublist in
> order to get the 1st column of the original data file?

map head does this. More general,

import Data.List

and then

transpose

does what you'd expect, so

firstColumn xss = head (transpose xss)

can do it.

But there's a catch.
As a rule, don't use head.

head is a partial function, if it's called with an empty list, it throws an 
error. And that error isn't particularly informative. So if you have 
multiple calls to head in your programme and one of them throws an error, 
it can be a major pain to find out what went wrong (and sometimes an empty 
list where a nonempty one is expected isn't an unrecoverable error, in 
those cases using head is wrong in an additional way).
It's okay to use head in places where it is known that it can't be called 
on an empty list because its argument comes from a function that produces 
only nonempty lists (some people disagree with that opinion and think head 
should never be called, on principle or because of a feared slippery 
slope).
But in general, don't use head.

So, how do we do it without head?

For example,

-- default value if there is no sublist
sublistLength [] = 0
sublistLength (xs : _) = length xs

Or use Maybe to indicate whether it's a real result or not,

sublistLength [] = Nothing
sublistLength (xs : _) = Just (length xs)

{-
import Data.Maybe

sublistLength = fmap length . listToMaybe
-}

-- skip empty sublists
firstColumn xss = [x | (x : _) <- xss]

-- indicate empty sublists with Maybe
import Data.Maybe -- for listToMaybe
firstColumn xss = map listToMaybe xss

That loses a little conciseness, but it buys you safety.

>
> Many thanks
>
> Lorenzo


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

Message: 3
Date: Thu, 9 Sep 2010 20:41:06 +0100
From: Gaius Hammond <ga...@gaius.org.uk>
Subject: Re: [Haskell-beginners] specify type for a function in GHCi
To: Henry Olders <henry.old...@mcgill.ca>
Cc: Beginners@haskell.org
Message-ID: <c6a16917-dc5a-4ee8-8974-b27530c13...@gaius.org.uk>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes


On 9 Sep 2010, at 15:32, Henry Olders wrote:

> Is there a way to specify the type for a function inside GHCi, that  
> is, without creating a separate file and loading it in?



I don't think there is, but you can say :r to reload the current module.



Cheers,



G







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

Message: 4
Date: Fri, 10 Sep 2010 02:29:56 -0700
From: Ryan Prichard <ryan.prich...@gmail.com>
Subject: [Haskell-beginners] Stack space overflow with foldl'
To: beginners@haskell.org
Message-ID: <20100910022956.010e0...@ryan>
Content-Type: text/plain; charset=US-ASCII

Hi,

I see a stack overflow with this code, but I don't understand why.

Test.hs
--------------

main :: IO ()
main = do
  let list = ([1..3*1000*1000] :: [Int])
  let !x = foldl'2 (flip seq) () list
  return x

-- foldl'2 is just the __GLASGOW_HASKELL__ version of Data.List.foldl'.
-- http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/
-- src/Data-List.html
-- The test case behaves the same if I call foldl' instead of foldl'2.
foldl'2 :: (a -> b -> a) -> a -> [b] -> a
foldl'2 f z0 xs0 = lgo z0 xs0
    where lgo z []     = z
          lgo z (x:xs) = let z' = f z x in z' `seq` (lgo z' xs)

$ ghc Test.hs -o Test
$ time ./Test

real    0m0.087s
user    0m0.084s
sys     0m0.000s

$ ghc Test.hs -o Test -O
$ time ./Test
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.

real    0m1.780s
user    0m1.764s
sys     0m0.004s

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.3

I looked at the Core output with ghc-core, with or without
optimizations, and I see a $wlgo recursive function that doesn't appear
to end in a tail call.  I don't see any let expressions in the
folding code, so I assume no thunks are being created.  I can make a
tail call appear by doing either of two things:

1. Replace "lgo z []" with "lgo !z []".  This suggestion came from an
email on haskell-beginners that I can't find right now.  It was a few
months ago.

2. Instead of using the __GLASGOW_HASKELL__ version of foldl', use the
other version:

foldl' f a []     = a
foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs

My test case is contrived.  Originally, I had a program that read
lines from a file as Data.ByteString values, and I was trying to debug
a stack overflow.  I added the foldl' call to force the evaluation of
the ByteString lines, but the foldl' call itself overflowed the
stack.

I might have fixed my original stack overflow problem.  I was applying
sum to a large list of integers, and sum is lazy.  I don't think I have
any real code anymore that overflows the stack, but I'm uncomfortable
because I don't know why my test case doesn't work.

Is the foldl' call in my test case allowed to have linear stack usage?

Thanks,
-Ryan


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

Message: 5
Date: Fri, 10 Sep 2010 08:02:48 -0400
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: [Haskell-beginners] Network programming (select(2)) for
        Haskell?
To: beginners <beginners@haskell.org>
Message-ID:
        <aanlktimfemts0hb2srtvtk-jy7935jaoboeqo_ufn...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Recently I've written a Perl program that "bridges" 2 rsync commands
together. It basically allows remote-to-remote rsyncs from a "control"
server .
The code is here: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=29852#a29852

In order to get better at network programming using haskell, I'd like
to try and port it to Haskell, but I can't seem to locate the
equivalent to select(2) in Haskell. Perhaps a different idiom is to be
used?

These kinds of scripts a pretty much the bread and butter of my daily
work, and understanding network programming of this type in Haskell
would really help me integrate Haskell into my workplace. Can anyone
offer pointers for me to get started?


Thanks a lot,

Patrick

-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 6
Date: Fri, 10 Sep 2010 14:35:49 +0200
From: Paolo Losi <paolo.l...@gmail.com>
Subject: [Haskell-beginners] Re: Debugging overlapping instances (mtl
        vs      transformers)
To: beginners@haskell.org
Message-ID: <i6d8nq$oi...@dough.gmane.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Thanks Stephen,

On 09/09/2010 18:07, Stephen Tetley wrote:
> What package are you trying build? - at a pinch it looks like it has
> conflicting dependencies for the packages 'transformers' and mtl.

It was a (cabalized) project of mine.

I finally solved the problem.

My project depends on transformers and HTTP packages.
When you import Network.HTTP, it brings into scope:

instance Monad (Either e)

What I discovered is that:

- Not only the instance is in scope in the module that imports HTTP
   but in all project modules. That means that I cannot use
   Either as Monad Error in my modules (since that would trigger
   the overlapping instances error because it would conflict
   with transformers' instance).

- you cannot hide class instances for imported modules

- I was getting the overalapping instance error in a module
   that didn't import Network.HTTP. This is somewhat misleading
   because the imports in that module seemed safe.

- There is no way to discover quickly / automatically which
   import is causing the overlap. The best that I could do
   was to check the imports one by one in ghci and check with
   :info Either Either's instances. Is there a better/quicker way?

I'm reporting my finding to ask for a confirmation or get
advices on how to generally cope with this kind of errors.

Thanks
Paolo



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

Message: 7
Date: Fri, 10 Sep 2010 15:08:29 +0200
From: Johan Tibell <johan.tib...@gmail.com>
Subject: Re: [Haskell-beginners] Network programming (select(2)) for
        Haskell?
To: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <aanlkti=ugcavk9g1d753q=9v3dwcs0j57q+qsuyav...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi Patrick,

On Fri, Sep 10, 2010 at 2:02 PM, Patrick LeBoutillier <
patrick.leboutill...@gmail.com> wrote:

> In order to get better at network programming using haskell, I'd like
> to try and port it to Haskell, but I can't seem to locate the
> equivalent to select(2) in Haskell. Perhaps a different idiom is to be
> used?
>

GHC uses select/epoll/kqueue internally to multiplex its lightweight
threads. When you would use a select loop for handle many connections in one
language you would typically launch one thread per connection in Haskell,
using forkIO. With the upcoming version GHC can handle 100k+ threads this
way. Right now the limit is 1024 simultaneous connections due a hard coded
limit in select.

You code would look something like this:

acceptConnections serverSock = do
    sock <- accept severSock
    forkIO $ handleConnection sock
    acceptConnections serverSock

handleConnection sock = do
    msg <- recv
    send ...
    sClose sock

I'd recommend using the network-bytestring library which provides more
efficient versions of e.g. recv and send.

Cheers,
Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100910/667bebae/attachment-0001.html

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

Message: 8
Date: Fri, 10 Sep 2010 15:19:44 +0200
From: Paolo Losi <paolo.l...@gmail.com>
Subject: [Haskell-beginners] Re: Network programming (select(2)) for
        Haskell?
To: beginners@haskell.org
Message-ID: <4c8a3070.9080...@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Patrick,

On 10/09/2010 14:02, Patrick LeBoutillier wrote:
> In order to get better at network programming using haskell, I'd like
> to try and port it to Haskell, but I can't seem to locate the
> equivalent to select(2) in Haskell. Perhaps a different idiom is to be
> used?

No select(2) syscall is explicitly available at API level.
This is because of how the Haskell (GHC) Runtime is structured.
Both the default single threaded runtime that the multi-threaded
runtime are capable of hosting different haskell light-threads.
IO between lightweight threads is multiplex transparently
by the runtime via select(2) o epoll/kqueue in the upcoming ghc.
Haskell lightweight threads are very cheap and efficient
so the idiomatic way to handle your use case is to spawn different
threads with forkIO (you are forking lightweight threads not OS thread)
and you some Haskell concurrency primitive for thread
communication/synchronization (see MVar and Chan in Control.Concurrent).

Paolo



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

Message: 9
Date: Fri, 10 Sep 2010 16:10:46 +0200
From: John Obbele <john.obb...@gmail.com>
Subject: [Haskell-beginners] Cabal and .chs modules dependencies
To: beginners@haskell.org
Message-ID: <20100910141046.ga3...@megabook.localdomain>
Content-Type: text/plain; charset=us-ascii

Hi everyone !

I'm stuck with a cabal quirk concerning modules build with c2hs:
the modules declared in {exposed,others}-modules are build
sequentially *and* the exposed modules are processed first,
followed by the "other-modules".

So if I have a Foobar.Quux.MyModule which depends of
Foobar.MyInternal.Foo1(.chs) and write in *.cabal:

>   library
>     Other-Modules:     Foobar.MyInternal.Foo0
>                        Foobar.MyInternal.Foo1
>
>     Exposed-Modules:   Foobar.Quux.MyModule

or 

>   library
>     Other-Modules:     Foobar.MyInternal.Foo0
>
>     Exposed-Modules:   Foobar.Quux.MyModule
>                        Foobar.MyInternal.Foo1

I get the following error:

    bash $ cabal clean && cabal configure && cabal build
    cabal clean                                              
    cleaning...                                              
    cabal configure
    Resolving dependencies...                                
    Configuring foobar-0.0.0.1
    cabal build                                              
    Preprocessing library foobar-0.0.0.1...                  
    c2hs: Foobar.MyInternal.Foo1.chi not found in:    
    dist/build                                               

The only solution I have for now is to write:

>   library
>     Exposed-Modules:   Foobar.MyInternal.Foo1
>                        Foobar.Quux.MyModule
>
>     Other-Modules:     Foobar.MyInternal.Foo0

... which unfortunately exposes "Foobar.MyInternal.Foo1". Does
anyone know how *not* to expose Foobar.MyInternal.* ?

(by the way I am using those versions:
cabal-install version 0.8.2
using version 1.8.0.2 of the Cabal library 
C->Haskell Compiler, version 0.16.2 Crystal Seed, 24 Jan 2009
  build platform is "i386-linux" <1, True, True, 1>)

regards,
/John


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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 22
*****************************************

Reply via email to