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. partitionM (Tom Tobin)
2. Re: partitionM (Daniel Fischer)
3. Re: fail: wxhaskell with cabal (Josh Bronson)
4. Re: partitionM (Daniel Fischer)
5. Re: Eq a => [a]->[a] (Daniel Fischer)
6. "reusable" data in Haskell (John Velman)
7. Re: "reusable" data in Haskell (Stephen Tetley)
8. Re: "reusable" data in Haskell (John Velman)
9. Indenting (John Moore)
----------------------------------------------------------------------
Message: 1
Date: Thu, 3 Dec 2009 14:33:53 -0600
From: Tom Tobin <[email protected]>
Subject: [Haskell-beginners] partitionM
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
While working on some filesystem traversal code, I found myself
wanting to use the 'partition' function from Data.List, but with the
function 'doesDirectoryExist' in System.Directory (with type Filepath
-> IO Bool). I noticed that there was no 'partitionM' in
Control.Monad, so I set out to write one.
Here's what I ended up with:
> import Control.Monad (foldM)
> partitionMHelper :: Monad m => (a -> m Bool) -> ([a],[a]) -> a -> m ([a],[a])
> partitionMHelper p (ts,fs) x = do
> b <- p x
> return (if b then (x:ts,fs) else (ts,x:fs))
> partitionM :: (Monad m) => (a -> m Bool) -> [a] -> m ([a], [a])
> partitionM p xs = foldM (partitionMHelper p) ([],[]) $ reverse xs
This works for my trivial cases, but can fail with extremely large
lists (not to mention being unable to take an infinite list and work
properly when passed to something like 'take 5'). Is there a way to
change the function to avoid having to traverse to the end of the list
(via reverse) just to get the output in the proper order? I started
trying to write a "foldrM", but haven't gotten anywhere useful yet.
Can someone point me in the right direction?
------------------------------
Message: 2
Date: Thu, 3 Dec 2009 22:06:19 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] partitionM
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Donnerstag 03 Dezember 2009 21:33:53 schrieb Tom Tobin:
> While working on some filesystem traversal code, I found myself
> wanting to use the 'partition' function from Data.List, but with the
> function 'doesDirectoryExist' in System.Directory (with type Filepath
> -> IO Bool). I noticed that there was no 'partitionM' in
> Control.Monad, so I set out to write one.
>
> Here's what I ended up with:
> > import Control.Monad (foldM)
> >
> > partitionMHelper :: Monad m => (a -> m Bool) -> ([a],[a]) -> a -> m
> > ([a],[a])
Before thinking much about it, I believe, a lazy pattern would help:
partitionHelper p ~(ts,fs) x = do ...
The reverse in partitionM still forbids infinite lists, I'll come to that later.
> > partitionMHelper p (ts,fs) x = do
> > b <- p x
> > return (if b then (x:ts,fs) else (ts,x:fs))
> >
> > partitionM :: (Monad m) => (a -> m Bool) -> [a] -> m ([a], [a])
> > partitionM p xs = foldM (partitionMHelper p) ([],[]) $ reverse xs
>
> This works for my trivial cases, but can fail with extremely large
> lists (not to mention being unable to take an infinite list and work
> properly when passed to something like 'take 5'). Is there a way to
> change the function to avoid having to traverse to the end of the list
> (via reverse) just to get the output in the proper order? I started
> trying to write a "foldrM", but haven't gotten anywhere useful yet.
>
> Can someone point me in the right direction?
------------------------------
Message: 3
Date: Thu, 3 Dec 2009 17:20:28 -0600
From: Josh Bronson <[email protected]>
Subject: Re: [Haskell-beginners] fail: wxhaskell with cabal
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I think the Ubuntu 9.10 packages are up-to-date, so I'll try using
gtk2hs with the Ubuntu packages next time I have some free time.
Thanks for the help!
Josh
On Fri, Nov 27, 2009 at 4:18 PM, Daniel Fischer
<[email protected]> wrote:
> Am Freitag 27 November 2009 22:22:03 schrieb Josh Bronson:
>> Ah. Is there a better GUI library to work with?
>
> I don't know which one is 'better' for what purposes, but gtk2hs is much used.
> It is unfortunately, as far as I know, not cabalised, so you'd have to
> manually install
> it.
>
>> Is cabal along with
>> the Haskell Platform a good idea? Or is it better to just stick with
>> libraries that install natively using APT on Ubuntu? I'm just trying
>
> Don't know if it's still true, but the debian packages used to lag far behind
> hackage, so
> that used to not be a serious option. If nowadays the debian-haskell
> packagers are up to
> speed, apt-get has advantages over cabal install.
>
>> to write a little GUI application (John Conway's Game of Life) to
>> learn Haskell.
>>
>> Thanks,
>> Josh
>>
>> On Fri, Nov 27, 2009 at 1:07 PM, Daniel Fischer
>>
>> <[email protected]> wrote:
>> > Which reminds me, I've *never* succeeded building wxhaskell, I wonder how
>> > many managed.
>
> This is now no longer true. I read the header "dialup.h" and replaced the
> "undeclared"
> macros wxEVT_DIALUP_(DIS)CONNECTED with the appropriate values in
> eljevent.cpp, then
> wxcore built and afterwards also wx. I haven't tested it, so it may be
> broken, but if it
> is, that shouldn't be the cause, I think.
>
------------------------------
Message: 4
Date: Fri, 4 Dec 2009 00:53:17 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] partitionM
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Donnerstag 03 Dezember 2009 22:06:19 schrieb Daniel Fischer:
> Am Donnerstag 03 Dezember 2009 21:33:53 schrieb Tom Tobin:
> > While working on some filesystem traversal code, I found myself
> > wanting to use the 'partition' function from Data.List, but with the
> > function 'doesDirectoryExist' in System.Directory (with type Filepath
> > -> IO Bool). I noticed that there was no 'partitionM' in
> > Control.Monad, so I set out to write one.
> >
> > Here's what I ended up with:
> > > import Control.Monad (foldM)
> > >
> > > partitionMHelper :: Monad m => (a -> m Bool) -> ([a],[a]) -> a -> m
> > > ([a],[a])
>
> Before thinking much about it, I believe, a lazy pattern would help:
>
> partitionHelper p ~(ts,fs) x = do ...
First, typo, bad me.
Second, no it won't, we're doing a left fold (sort of), so it's already a good
pair.
>
> The reverse in partitionM still forbids infinite lists, I'll come to that
> later.
>
> > > partitionMHelper p (ts,fs) x = do
> > > b <- p x
> > > return (if b then (x:ts,fs) else (ts,x:fs))
> > >
> > > partitionM :: (Monad m) => (a -> m Bool) -> [a] -> m ([a], [a])
> > > partitionM p xs = foldM (partitionMHelper p) ([],[]) $ reverse xs
> >
> > This works for my trivial cases, but can fail with extremely large
> > lists (not to mention being unable to take an infinite list and work
> > properly when passed to something like 'take 5').
Can't, in general. What if some test at the end fails
(p x = [], resp Nothing, ioError (userError "too bad"))?
Then, for many monads the whole computation must fail, so you can't start
returning
anything before you know there is something to return.
> > Is there a way to change the function to avoid having to
> > traverse to the end of the list (via reverse) just to get the
> > output in the proper order?
Yes,
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a],[a])
partitionM p xs = do
(f,g) <- pMHelper p xs
return (f [], g [])
pMHelper :: Monad m => (a -> m Bool) -> [a] -> m ([a] -> [a],[a] -> [a])
pMHelper p xs = foldM help (id,id) xs
where
help (f,g) x = do
b <- p x
return (if b then (f . (x:),g) else (f,g . (x:)))
It's a little slower than yours for my test, though and I don't see how to
speed it up.
> > I started trying to write a "foldrM",
> > but haven't gotten anywhere useful yet.
> >
> > Can someone point me in the right direction?
------------------------------
Message: 5
Date: Fri, 4 Dec 2009 02:37:11 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Eq a => [a]->[a]
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 03 Dezember 2009 20:00:00 schrieb Daniel Fischer:
> You can remove one factor (log k) by checking the size instead of
> membership:
>
> f xs = map fst . takeWhile snd . zip xs . zipWith ((. S.size) . (==)) [1 ..
> ] $ cums where
> cums = tail $ scanl (flip S.insert) S.empty xs
>
> I think for *short* nubbed prefixes, Brent's version is faster, but I've no
> idea yet when short stops (10, 100, 1000?).
Couldn't measure a difference for short lists, starts to become faster than
Brent's
between 10^5 and 10^6. The manual loop
f :: Ord a => [a] -> [a]
f xs = go 1 S.empty xs
where
go i s (y:ys)
| i == S.size s' = y:go (i+1) s' ys
| otherwise = []
where
s' = S.insert y s
go _ _ _ = []
is ~15% faster.
------------------------------
Message: 6
Date: Thu, 3 Dec 2009 19:52:49 -0800
From: John Velman <[email protected]>
Subject: [Haskell-beginners] "reusable" data in Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
In prolog, one can create a database of facts and predicates, which,
depending on the prolog (AFAIK) can be 'compiled' and loaded quickly.
How does one do something comparable in Haskell?
I guess I have in mind something like a function that accesses the data
with a key. And ideally, the data should itself be able to contain
functions.
I know there is a hash table library, and interfaces to databases like
postgresql. If using hash table, is there a way to 'pickle' the hash
table, and unpickle it?
I'd be happy to have some pointers to someplace that this has been done (or
some documentation on how to do it.
Thanks,
John Velman
------------------------------
Message: 7
Date: Fri, 4 Dec 2009 08:42:11 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] "reusable" data in Haskell
To: John Velman <[email protected]>, [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hello John
Data-Binary is the commonly used serialization library, it could well
be adequate for your purposes:
http://hackage.haskell.org/package/binary
>From the blurb:
"Serialisation speeds of over 1 G/sec have been observed, so this
library should be suitable for high performance scenarios."
It really depends how you choose to represent the database, if you
have cycles serialization could be tricky, but if you just have atoms
of strings for the predicates and facts it should be fine(*).
Best wishes
Stephen
(*) Converting to and from Strings and ByteStrings will take _some_
time in the serialization layer, but it would take some time from a
real database or other format.
------------------------------
Message: 8
Date: Fri, 4 Dec 2009 08:34:14 -0800
From: John Velman <[email protected]>
Subject: Re: [Haskell-beginners] "reusable" data in Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks, Stephen. Looks like this is pretty much what I'm looking for.
Best,
John V.
On Fri, Dec 04, 2009 at 08:42:11AM +0000, Stephen Tetley wrote:
> Hello John
>
> Data-Binary is the commonly used serialization library, it could well
> be adequate for your purposes:
>
> http://hackage.haskell.org/package/binary
>
> >From the blurb:
> "Serialisation speeds of over 1 G/sec have been observed, so this
> library should be suitable for high performance scenarios."
>
> It really depends how you choose to represent the database, if you
> have cycles serialization could be tricky, but if you just have atoms
> of strings for the predicates and facts it should be fine(*).
>
>
>
> Best wishes
>
> Stephen
>
> (*) Converting to and from Strings and ByteStrings will take _some_
> time in the serialization layer, but it would take some time from a
> real database or other format.
------------------------------
Message: 9
Date: Fri, 4 Dec 2009 20:26:52 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] Indenting
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
I'm a bit stuck. I want to indent my answer by using a count function
to indent my answer. Say for example I want to show the levels in a simple
arithmethic expression.
like 3*(4+5)
I want to be able to print out :
4+5 = 9
3*9 = 27 (notice the indentation as I go through an expression.
I tried this but it a mess, I tried to use prettyprint but this seemed very
complicated.
Any suggestions greatly appreciated
type Indent = (Int)
spacing :: Int -> Indent
spacing n = Nothing
spacing n (x:xs) = if (n==1)
then (Just n)
else spacing n xs
where (n > 1)
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20091204/433a6e49/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 4
****************************************