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: define action getInt like getLine (Daniel Fischer)
2. Re: hunit question (Daniel Fischer)
3. Re: Problems installing TagSoup.Parsec (Mark Wong-VanHaren)
4. Re: Problems installing TagSoup.Parsec (Daniel Fischer)
5. Cure me of my OOP ways (David Gordon)
6. Re: Cure me of my OOP ways
(Stephen Blackheath [to Haskell-Beginners])
7. Fast tree manipulation (Gabi)
----------------------------------------------------------------------
Message: 1
Date: Tue, 2 Feb 2010 23:16:01 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Dienstag 02 Februar 2010 23:06:41 schrieb legajid:
> -- Définition de la fonction de calcul
> f Nothing = -9999
> f (Just x) = 2 * x
That's already in the standard libs:
ghci> :t maybe
maybe :: b -> (a -> b) -> Maybe a -> b
ghci> maybe (-9999) (*2) (Just $ fromEnum 'k')
214
------------------------------
Message: 2
Date: Tue, 2 Feb 2010 23:37:18 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] hunit question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Dienstag 02 Februar 2010 23:03:48 schrieb Daniel Fischer:
> Am Dienstag 02 Februar 2010 22:46:20 schrieb Joe Van Dyk:
> >
> > Any chance anyone can show me how to write a QuickCheck test for this?
> >
> > Joe
>
> prop_binary_search :: Int -> [Int] -> Bool
> prop_binary_search x xs =
> let ys = sort xs
> in binary_search x ys == elemIndex x ys
>
>
> ghci> quickCheck prop_binary_search
> +++ OK, passed 100 tests.
Ah, but:
ghci> quickCheckWith (stdArgs { maxSuccess = 10000, maxDiscard = 1000,
maxSize = 2000 }) prop_binary_search
*** Failed! Falsifiable (after 8007 tests and 3 shrinks):
-1
[-1,-1]
Okay, does that mean my function is wrong?
In this case, no, my test was wrong.
I intended binary_search to be used only on *strictly* ascending lists, so
change the test to
import Data.IntSet (fromList, toAscList)
prop_binary_search :: Int -> [Int] -> Bool
prop_binary_search x xs = let ys = toAscList (fromList xs)
in binary_search x ys == elemIndex x ys
Now:
ghci> quickCheckWith (stdArgs { maxSuccess = 10000, maxDiscard = 1000,
maxSize = 2000 }) prop_binary_search
+++ OK, passed 10000 tests.
or perhaps a different test:
prop_binary_search_2 :: Int -> [Int] -> Bool
prop_binary_search_2 x xs =
let ys = sort xs
in case binary_search x ys of
Nothing -> x `notElem` ys
Just k -> x == ys !! k
------------------------------
Message: 3
Date: Tue, 2 Feb 2010 16:19:49 -0800
From: Mark Wong-VanHaren <[email protected]>
Subject: Re: [Haskell-beginners] Problems installing TagSoup.Parsec
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi, Daniel-
Many thanks for your help. Your instructions were very clear and
worked perfectly.
The only snag I encountered was a separate problem I had created for
myself, which I'll mention here in case it may help someone else...
I (thought I) wanted to uninstall TagSoup 0.8. (I didn't really need
to.) Cabal doesn't provide an "uninstall" command, so I naively
attempted to do it "manually" -- that is, to simply rm
TagSoup-0.8-related files in my installation.
This confused Cabal, which still "believed" (by virtue of config
settings somewhere, presumably) that TagSoup was still installed. So
cabal failed to find the actual files needed and complained.
Rather than manually rm-ing things, the proper solution (I believe --
at least it worked for me) was to use "ghc-pkg unregister tagsoup".
Anyway, thanks again for your help, Daniel. Cheers,
-Mark
2010/2/1 Daniel Fischer <[email protected]>:
> Am Montag 01 Februar 2010 21:21:12 schrieb Mark Wong-VanHaren:
>> Hi, folks-
>>
>> I'm a Haskell newbie. There's a very real chance my question has a
>> simple answer.
>
> Yes and no. The problem is that the build-depends field of tagsoup-
> parsec.cabal doesn't specify an upper bound on the version of tagsoup to
> use. Thus cabal tries to build it with the latest and greates tagsoup, 0.8.
> Neill Mitchell changed the tagsoup API, bad .cabal files break.
>
> Fix: unpack tagsoup-parsec-0.0.6, open the .cabal file, change
>
>
> Library
> Build-Depends: base<5, tagsoup, parsec<3
>
> to
>
> Library
> Build-Depends: base<5, tagsoup < 0.7, parsec<3
>
> $ cd tagsoup-parsec-0.0.6
> $ cabal install
>
> email maintainer of tagsoup-parsec to fix it (adapt to new tagsoup API).
>
>>
>> I wish to use the library Text.HTML.TagSoup.Parsec (
>> http://hackage.haskell.org/package/tagsoup-parsec), which looks very
>> cool.
>>
>> When I attempt to install it, I get this error:
>>
>> # cabal install tagsoup-parsec
>> Resolving dependencies...
>> Configuring tagsoup-parsec-0.0.6...
>> Preprocessing library tagsoup-parsec-0.0.6...
>> Building tagsoup-parsec-0.0.6...
>> [1 of 1] Compiling Text.HTML.TagSoup.Parsec (
>> Text/HTML/TagSoup/Parsec.hs, dist/build/Text/HTML/TagSoup/Parsec.o )
>>
>> Text/HTML/TagSoup/Parsec.hs:37:5:
>> `Tag' is not applied to enough type arguments
>> Expected kind `*', but `Tag' has kind `* -> *'
>> In the type synonym declaration for `WholeTag'
>> cabal: Error: some packages failed to install:
>> tagsoup-parsec-0.0.6 failed during the building phase. The exception
>> was: exit: ExitFailure 1
>>
>>
>> It would seem to be a type mismatch between TagSoup.Parsec and the base
>> TagSoup (on which it depends).
>>
>> Suspecting that the version-number dependency information might be
>> incorrect, I tried uninstalling TagSoup and reinstalling an older
>> version. This didn't help.
>>
>> Can anyone steer me in the right direction?
>>
>> Thanks!
>> -Mark
>
>
------------------------------
Message: 4
Date: Wed, 3 Feb 2010 01:44:09 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Problems installing TagSoup.Parsec
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Mittwoch 03 Februar 2010 01:19:49 schrieb Mark Wong-VanHaren:
> Hi, Daniel-
>
> Many thanks for your help. Your instructions were very clear and
> worked perfectly.
>
> The only snag I encountered was a separate problem I had created for
> myself, which I'll mention here in case it may help someone else...
>
> I (thought I) wanted to uninstall TagSoup 0.8. (I didn't really need
> to.) Cabal doesn't provide an "uninstall" command, so I naively
> attempted to do it "manually" -- that is, to simply rm
> TagSoup-0.8-related files in my installation.
>
> This confused Cabal, which still "believed" (by virtue of config
> settings somewhere, presumably)
Cabal asks ghc-pkg what is installed, it doesn't keep a separate database.
> that TagSoup was still installed. So
> cabal failed to find the actual files needed and complained.
>
> Rather than manually rm-ing things, the proper solution (I believe --
> at least it worked for me) was to use "ghc-pkg unregister tagsoup".
Right.
First unregister the packages.
Then you can delete the files if you want to.
>
> Anyway, thanks again for your help, Daniel. Cheers,
> -Mark
------------------------------
Message: 5
Date: Tue, 2 Feb 2010 18:43:30 -0800
From: David Gordon <[email protected]>
Subject: [Haskell-beginners] Cure me of my OOP ways
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi Folks,
I am redesigning a system previously implemented in C++ using Haskell. I am
keen to adopt a native programming style, but certain things about the
original implementation still make perfect sense to me, including the
C++-style subtype polymorphism. I'd be grateful if someone could take a look
at the following sample and tell me if I'm missing some neater possible
implementation in Haskell.
{-# OPTIONS -fglasgow-exts #-}
-- for existential types
-- all subclasses take the same input format and can be created
factory-pattern style
data Input = Input Int Int
-- this is the class we have to implement when creating a 'subtype'
class InternalAPI i where
doitImpl :: i -> Input -> Int
-- factory function, everything is initialised by giving an "i" and the
Input
create :: i -> Input -> PublicAPI
create a b = PublicAPI a b
-- this is the object clients see, regardless of the underlying
implementation
data PublicAPI = forall p. InternalAPI p => PublicAPI p Input
doit (PublicAPI p i) = doitImpl p i
-- Here's an example implementation of something trivial
data Adder = Adder
instance InternalAPI Adder where
doitImpl _ (Input a b) = a + b -- first argument is superfluous
-- There's the factory function in use:
test = create Adder (Input 1 2)
result = doit test
I'm actually pretty happy with this. I'm not trying for a complete OOP
implementation but it lets me:
- have the 'base class' (InternalAPI) implement default versions of methods
with full access to the 'Input' structure containing the member data
- do further inheritance, since I can select the implementation of a method
using that first argument (e.g. on doitImpl)
- base and derived classes see the same member data
- Implementations of 'InternalAPI' can reference each other through the
PublicAPI interface.
Admittedly it's a rather vague question. I wonder if there are any articles
out there showing how to reformulate problems solved using C++-style object
models into Haskell programs? After all, it's hardly as if the C++ model is
particularly elegant...
thanks,
David
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100202/b6738d4c/attachment-0001.html
------------------------------
Message: 6
Date: Wed, 03 Feb 2010 19:19:14 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Cure me of my OOP ways
To: David Gordon <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
David,
Here's the same code in more idiomatic Haskell:
type API = Int -> Int -> Int
test = 1 + 2
result = test
I am being a smartypants, but only partly - I'm trying to make the point
that partial application essentially replaces the factory pattern from
OO. If you want to make another subclass, that has some extra internal
data, e.g. 'add then scale by some value'...
addAndScale :: Int -> API
addAndScale f a b = (a + b) * f
as10 = addAndScale 10
result = as10 3 5
These functions can't modify their internal state. If you want to add
that capability, there are several ways. Here is one:
newtype API = API {unAPI :: Int -> Int -> (Int, API) }
sumTwo :: Int -> API
sumTwo total = API $ \a b ->
let total' = total + a + b
in (total', sumTwo total')
result =
let s1 = sumTwo 0
(_, s2) = unAPI s1 2 2
(total, s3) = unAPI s2 3 7
in total
This returns a new API, which the caller should now use instead of the
old one, and this allows it to maintain state. The reason for the
newtype is that things defined with 'type' can't be recursive.
Steve
David Gordon wrote:
> Hi Folks,
>
> I am redesigning a system previously implemented in C++ using Haskell. I
> am keen to adopt a native programming style, but certain things about
> the original implementation still make perfect sense to me, including
> the C++-style subtype polymorphism. I'd be grateful if someone could
> take a look at the following sample and tell me if I'm missing some
> neater possible implementation in Haskell.
>
> {-# OPTIONS -fglasgow-exts #-}
> -- for existential types
>
> -- all subclasses take the same input format and can be created
> factory-pattern style
> data Input = Input Int Int
>
> -- this is the class we have to implement when creating a 'subtype'
> class InternalAPI i where
> doitImpl :: i -> Input -> Int
>
> -- factory function, everything is initialised by giving an "i" and
> the Input
> create :: i -> Input -> PublicAPI
> create a b = PublicAPI a b
>
> -- this is the object clients see, regardless of the underlying
> implementation
> data PublicAPI = forall p. InternalAPI p => PublicAPI p Input
> doit (PublicAPI p i) = doitImpl p i
>
> -- Here's an example implementation of something trivial
> data Adder = Adder
> instance InternalAPI Adder where
> doitImpl _ (Input a b) = a + b -- first argument is superfluous
>
> -- There's the factory function in use:
> test = create Adder (Input 1 2)
> result = doit test
>
> I'm actually pretty happy with this. I'm not trying for a complete OOP
> implementation but it lets me:
> - have the 'base class' (InternalAPI) implement default versions of
> methods with full access to the 'Input' structure containing the member data
> - do further inheritance, since I can select the implementation of a
> method using that first argument (e.g. on doitImpl)
> - base and derived classes see the same member data
> - Implementations of 'InternalAPI' can reference each other through the
> PublicAPI interface.
>
> Admittedly it's a rather vague question. I wonder if there are any
> articles out there showing how to reformulate problems solved using
> C++-style object models into Haskell programs? After all, it's hardly as
> if the C++ model is particularly elegant...
>
> thanks,
>
> David
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 7
Date: Wed, 3 Feb 2010 10:50:57 +0200
From: Gabi <[email protected]>
Subject: [Haskell-beginners] Fast tree manipulation
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi
I need to manipulate trees in great numbers and frequencies (lots of
trees, lots of manipulations).
What would be the right approach to achieve this and get good
performance ? Maybe a library already exists ?
--
Regards,
Gabi
http://bugspy.net
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 7
****************************************