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.  testing and the culture of Haskell (Michael Easter)
   2. Re:  testing and the culture of Haskell (Isaac Dupree)
   3. Re:  testing and the culture of Haskell (Stephen Tetley)
   4.  problems using bytestrings (Joe Van Dyk)
   5. Re:  problems using bytestrings (Daniel Fischer)
   6. Re:  problems using bytestrings (Joe Van Dyk)
   7. Re:  problems using bytestrings (Daniel Fischer)


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

Message: 1
Date: Mon, 18 Jan 2010 10:04:33 -0600
From: Michael Easter <[email protected]>
Subject: [Haskell-beginners] testing and the culture of Haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Folks,

A friend asked me about Test-Driven Development (TDD) within the FP
community. I realize
the FP community is larger than a Haskell mailing list, but I wanted to get
a sense from this
camp.

I'm a fan of Haskell but I can't say that I know the culture yet. That said,
I'm writing as *a fan* and
not a critic. These are genuine, earnest questions.

Q: Is TDD advocated the Haskell community? Is it controversial? Is it even
on the radar?

Q: If TDD is not advocated: why? Is there something about the Zen of Haskell
development that
is an impedance mismatch with TDD?

Q: Note that TDD and "writing tests" are different things. With respect to
"writing tests", I know
that HUnit exists and that RWH has a chapter on quality assurance.

Given that, I'd like to know: how widely is HUnit used? If you were to start
a new Haskell project, would you
include HUnit (a) immediately (b) eventually (c) maybe (d) another adjective
?


sincerely
Michael Easter

-- 
----------------------
Michael Easter
http://codetojoy.blogspot.com -> Putting the thrill back in blog

http://twitter.com/patentlyfalse -> Satirical tech headlines (and nothing
but)

http://youtube.com/ocitv -> Fun people doing serious software engineering
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100118/00270ddb/attachment-0001.html

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

Message: 2
Date: Mon, 18 Jan 2010 11:19:35 -0500
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] testing and the culture of Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Michael Easter wrote:
> Q: Is TDD advocated the Haskell community? Is it controversial? Is it even
> on the radar?

we're not big on hyped, capitalized words, but many of us like tests. 
There's even a tool, "HPC" (Haskell Program Coverage) that you can use 
when you run your tests to see how much of the code is reached at all by 
the tests.

Actually, in terms of writing tests before writing code (which I 
sometimes do)... what Haskellers often do even more often is to write 
type-signature for functions before they write the function's code. 
This helps testing (i.e. test failure = compile-time type errors) as 
well as thinking about what you want the function to do.  Types are 
generally more useful-for-thinking in FP than in imperative languages

> Q: Note that TDD and "writing tests" are different things. With respect to
> "writing tests", I know
> that HUnit exists and that RWH has a chapter on quality assurance.

I would use QuickCheck first... have you taken a look at it?

-Isaac


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

Message: 3
Date: Mon, 18 Jan 2010 17:20:14 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] testing and the culture of Haskell
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello Michael

How about - Type Driven Development?

Personally, I like to work out the type a function should have, then
set about writing it. So I usually start from the type signature and
stub out the right-hand side of the definition (i.e. all the function
after the equals sign) with 'undefined'. Working like this is far from
universal in functional programming - it would be unnatural in Scheme
for instance, though some people using PLT Scheme appear to start
development by writing contract signatures. It certainly isn't
universal in Haskell either - some people like to write their
functions then fill in the type signature afterwards (if you do it
this way, providing your function compiles, you can query its 'most
general' type at GHCi's prompt).

ML used to have the proviso that type checking was always guaranteed
to terminate so type signatures were discretionary, I'm not sure if
this is still the case with O'Caml as it now seems to have quite an
extended type system. Certain bits of Haskell need explicit type
signatures - one example is polymorphic recursion.

I suspect very few people programming in Haskell use tests as the
primary 'driver' of their program designs (my interpretation of Test
Driven Developments slogans is that testing very much drives design).
I'd guess quite a number of people develop 'in tandem' with QuickCheck
(rather than strictly 'first' with QuickCheck) - for example, there is
a blog post on the XMonad window system covering how the authors
worked in this way.

http://cgi.cse.unsw.edu.au/~dons/blog/2007/05/01

Best wishes

Stephen


> I'm a fan of Haskell but I can't say that I know the culture yet.

PS. I'd like to think Haskell has at least 'cultures' (or schools,
factions?) rather than a unified culture or way. If you like
developing test-first, why change if it works.


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

Message: 4
Date: Tue, 19 Jan 2010 11:40:38 -0800
From: Joe Van Dyk <[email protected]>
Subject: [Haskell-beginners] problems using bytestrings
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hello,

I wrote a function that finds anagrams in a file.  It worked great.
Until I tried to use Bytestrings to get better performance.

Here's the code:
(http://github.com/joevandyk/haskell/blob/aa61a58e6a027dda60a32ae64ec99f92d00ae5ed/pearls/anagrams/anagram.hs)
import qualified Data.Map as Map
import Data.Ord
import Data.List
import qualified Data.ByteString.Char8 as BS

-- what's the type here?  I get an infinite type error
anagrams :: Ord a => [a] -> [a]
anagrams words =
  sorted_anagrams
  where
    sorted_anagrams      = sortBy (flip $ comparing length) get_anagrams
    get_anagrams         = Map.elems $ foldl' insert_word Map.empty words
    insert_word map word = Map.insertWith' (++) (sort word) [word] map

main = do
  -- original code, worked fine:
  -- input <- getContents
  -- print $ take 3 $ anagrams $ lines input
  -- new code with bytestring has errors
  input <- BS.getContents
  print $ take 3 $ anagrams $ BS.lines input


There are two errors.  One:
anagram.hs:7:0:
    Occurs check: cannot construct the infinite type: a = [a]
    When generalising the type(s) for `anagrams'


That happens when I add the type to the anagrams method.

If I don't have a type specified, then I get this error:
anagram.hs:21:30:
    Couldn't match expected type `[a]'
           against inferred type `BS.ByteString'
      Expected type: [[a]]
      Inferred type: [BS.ByteString]
    In the second argument of `($)', namely `BS.lines input'
    In the second argument of `($)', namely `anagrams $ BS.lines input'


I'm lost here.  Help!
-- 
Joe Van Dyk
http://fixieconsulting.com


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

Message: 5
Date: Tue, 19 Jan 2010 21:49:57 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] problems using bytestrings
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Dienstag 19 Januar 2010 20:40:38 schrieb Joe Van Dyk:
> Hello,
>
> I wrote a function that finds anagrams in a file.  It worked great.
> Until I tried to use Bytestrings to get better performance.
>
> Here's the code:
> (http://github.com/joevandyk/haskell/blob/aa61a58e6a027dda60a32ae64ec99f
>92d00ae5ed/pearls/anagrams/anagram.hs) import qualified Data.Map as Map
> import Data.Ord
> import Data.List
> import qualified Data.ByteString.Char8 as BS
>
> -- what's the type here?  I get an infinite type error
> anagrams :: Ord a => [a] -> [a]

anagrams :: Ord a => [[a]] -> [[[a]]]

But ask ghci, that's faster to answer such questions than the list :)

Don't give a type signature, load the module,

ghci> :t anagrams
anagrams :: Ord a => [[a]] -> [[[a]]]

> anagrams words =
>   sorted_anagrams
>   where
>     sorted_anagrams      = sortBy (flip $ comparing length) get_anagrams
>     get_anagrams         = Map.elems $ foldl' insert_word Map.empty
> words insert_word map word = Map.insertWith' (++) (sort word) [word] map

You sort each word from the input list, so word must have type

Ord a => [a],

thus the input has type

Ord a => [[a]].

The map you build maps Strings ([Char], generally, Ord a => [[a]]) to lists 
of Strings (so Map String [String] === Map [Char] [[Char]], in general, 
Ord a => Map [a] [[a]]), thus Map.elems gives a list of (lists of Strings), 
that is [[String]] === [[[Char]]], in general, [[[a]]].

>
> main = do
>   -- original code, worked fine:
>   -- input <- getContents
>   -- print $ take 3 $ anagrams $ lines input
>   -- new code with bytestring has errors
>   input <- BS.getContents
>   print $ take 3 $ anagrams $ BS.lines input
>
>
> There are two errors.  One:
> anagram.hs:7:0:
>     Occurs check: cannot construct the infinite type: a = [a]
>     When generalising the type(s) for `anagrams'
>
>
> That happens when I add the type to the anagrams method.
>
> If I don't have a type specified, then I get this error:
> anagram.hs:21:30:
>     Couldn't match expected type `[a]'
>            against inferred type `BS.ByteString'
>       Expected type: [[a]]
>       Inferred type: [BS.ByteString]
>     In the second argument of `($)', namely `BS.lines input'
>     In the second argument of `($)', namely `anagrams $ BS.lines input'
>
>
> I'm lost here.  Help!

ByteStrings are not lists, so Data.List.sort can't do anything with them.
You could change one line of the the code of anagrams to

insert_word map word = Map.insertWith' (++) (BS.sort word) [word] map

which restricts the type of anagrams to

anagrams :: [ByteString] -> [[ByteString]]

But, ByteStrings sort isn't good for short ByteStrings (allocate an array 
of 256 slots, count how often each character occurs, write in order to new 
ByteString - the overhead of allocating the array is larger than the 
sorting cost for short ByteStrings).

For ByteStrings as short as normal words in 
English/French/German/Italian/Spanish, it's much better to unpack the 
ByteStrings for sorting and change the line to

insert_word map word = Map.insertWith' (++) 
                        (BS.pack . sort . BS.unpack $ word) [word] map



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

Message: 6
Date: Tue, 19 Jan 2010 13:34:41 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] problems using bytestrings
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, Jan 19, 2010 at 12:49 PM, Daniel Fischer
<[email protected]> wrote:
> Am Dienstag 19 Januar 2010 20:40:38 schrieb Joe Van Dyk:
>> Hello,
>>
>> I wrote a function that finds anagrams in a file.  It worked great.
>> Until I tried to use Bytestrings to get better performance.
>>
>> Here's the code:
>> (http://github.com/joevandyk/haskell/blob/aa61a58e6a027dda60a32ae64ec99f
>>92d00ae5ed/pearls/anagrams/anagram.hs) import qualified Data.Map as Map
>> import Data.Ord
>> import Data.List
>> import qualified Data.ByteString.Char8 as BS
>>
>> -- what's the type here?  I get an infinite type error
>> anagrams :: Ord a => [a] -> [a]
>
> anagrams :: Ord a => [[a]] -> [[[a]]]
>
> But ask ghci, that's faster to answer such questions than the list :)
>
> Don't give a type signature, load the module,
>
> ghci> :t anagrams
> anagrams :: Ord a => [[a]] -> [[[a]]]
>
>> anagrams words =
>>   sorted_anagrams
>>   where
>>     sorted_anagrams      = sortBy (flip $ comparing length) get_anagrams
>>     get_anagrams         = Map.elems $ foldl' insert_word Map.empty
>> words insert_word map word = Map.insertWith' (++) (sort word) [word] map
>
> You sort each word from the input list, so word must have type
>
> Ord a => [a],
>
> thus the input has type
>
> Ord a => [[a]].
>
> The map you build maps Strings ([Char], generally, Ord a => [[a]]) to lists
> of Strings (so Map String [String] === Map [Char] [[Char]], in general,
> Ord a => Map [a] [[a]]), thus Map.elems gives a list of (lists of Strings),
> that is [[String]] === [[[Char]]], in general, [[[a]]].
>
>>
>> main = do
>>   -- original code, worked fine:
>>   -- input <- getContents
>>   -- print $ take 3 $ anagrams $ lines input
>>   -- new code with bytestring has errors
>>   input <- BS.getContents
>>   print $ take 3 $ anagrams $ BS.lines input
>>
>>
>> There are two errors.  One:
>> anagram.hs:7:0:
>>     Occurs check: cannot construct the infinite type: a = [a]
>>     When generalising the type(s) for `anagrams'
>>
>>
>> That happens when I add the type to the anagrams method.
>>
>> If I don't have a type specified, then I get this error:
>> anagram.hs:21:30:
>>     Couldn't match expected type `[a]'
>>            against inferred type `BS.ByteString'
>>       Expected type: [[a]]
>>       Inferred type: [BS.ByteString]
>>     In the second argument of `($)', namely `BS.lines input'
>>     In the second argument of `($)', namely `anagrams $ BS.lines input'
>>
>>
>> I'm lost here.  Help!
>
> ByteStrings are not lists, so Data.List.sort can't do anything with them.
> You could change one line of the the code of anagrams to
>
> insert_word map word = Map.insertWith' (++) (BS.sort word) [word] map
>
> which restricts the type of anagrams to
>
> anagrams :: [ByteString] -> [[ByteString]]
>
> But, ByteStrings sort isn't good for short ByteStrings (allocate an array
> of 256 slots, count how often each character occurs, write in order to new
> ByteString - the overhead of allocating the array is larger than the
> sorting cost for short ByteStrings).
>
> For ByteStrings as short as normal words in
> English/French/German/Italian/Spanish, it's much better to unpack the
> ByteStrings for sorting and change the line to
>
> insert_word map word = Map.insertWith' (++)
>                        (BS.pack . sort . BS.unpack $ word) [word] 
> map

Thanks for the response.  I was getting confused -- I'm used to
thinking about a String as being its own type, instead of being an
array of chars.

I now have:

anagrams :: [BS.ByteString] -> [[BS.ByteString]]
anagrams words =
  sorted_anagrams
  where
    sorted_anagrams      = sortBy (flip $ comparing length) get_anagrams
    get_anagrams         = Map.elems $ foldl' insert_word Map.empty words
    insert_word map word = Map.insertWith' (++) sorted_word [word] map
    sorted_word word     = BS.pack . sort . BS.unpack $ word


But get this error:

anagram.hs:12:27:
    No instance for (Ord (BS.ByteString -> BS.ByteString))
      arising from a use of `Map.insertWith'' at anagram.hs:12:27-69
    Possible fix:
      add an instance declaration for
      (Ord (BS.ByteString -> BS.ByteString))
    In the expression: Map.insertWith' (++) sorted_word [word] map
    In the definition of `insert_word':
        insert_word map word = Map.insertWith' (++) sorted_word [word] map
    In the definition of `anagrams':
        anagrams words
                   = sorted_anagrams
                   where
                       sorted_anagrams = sortBy (flip $ comparing
length) get_anagrams
                       get_anagrams = Map.elems $ foldl' insert_word
Map.empty words
                       insert_word map word = Map.insertWith' (++)
sorted_word [word] map
                       sorted_word word = BS.pack . sort . BS.unpack $ word


is sorted_word returning a function?


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

Message: 7
Date: Tue, 19 Jan 2010 23:11:20 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] problems using bytestrings
To: Joe Van Dyk <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Dienstag 19 Januar 2010 22:34:41 schrieb Joe Van Dyk:
>
> Thanks for the response.  I was getting confused -- I'm used to
> thinking about a String as being its own type, instead of being an
> array of chars.

Lists are NOT arrays.

If it was not just loose language on your side, learn the distinction.
List indexing (list !! k) is O(min k (length list)), array indexing is 
O(1).
An array knows its size (resp. it can easily be calculated from the known 
bounds), a list does not. length list is O(length list).

If you're not aware of the differences, sooner rather than later, you'll 
get bitten by a gigantic performance bug.

>
> I now have:
>
> anagrams :: [BS.ByteString] -> [[BS.ByteString]]
> anagrams words =
>   sorted_anagrams
>   where
>     sorted_anagrams      = sortBy (flip $ comparing length) get_anagrams
>     get_anagrams         = Map.elems $ foldl' insert_word Map.empty
> words insert_word map word = Map.insertWith' (++) sorted_word [word] map
> sorted_word word     = BS.pack . sort . BS.unpack $ word
>
>
> But get this error:
>
> anagram.hs:12:27:
>     No instance for (Ord (BS.ByteString -> BS.ByteString))
>       arising from a use of `Map.insertWith'' at anagram.hs:12:27-69
>     Possible fix:
>       add an instance declaration for
>       (Ord (BS.ByteString -> BS.ByteString))
>     In the expression: Map.insertWith' (++) sorted_word [word] map
>     In the definition of `insert_word':
>         insert_word map word = Map.insertWith' (++) sorted_word [word]
> map In the definition of `anagrams':
>         anagrams words
>                    = sorted_anagrams
>                    where
>                        sorted_anagrams = sortBy (flip $ comparing
> length) get_anagrams
>                        get_anagrams = Map.elems $ foldl' insert_word
> Map.empty words
>                        insert_word map word = Map.insertWith' (++)
> sorted_word [word] map
>                        sorted_word word = BS.pack . sort . BS.unpack $
> word
>
>
> is sorted_word returning a function?

sorted_word *is* a function.
What you want in insert_word is (sorted_word word), i.e.

insert_word map word = Map.insertWith' (++) (sorted_word word) [word] map

Since you forgot to apply sorted_word to the ByteString, it tries to use 
the function sorted_word :: ByteString -> ByteString as a key, but it can't 
find the

instance Ord (ByteString -> ByteString) where ...



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 15
*****************************************

Reply via email to