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.  Sorting arrays (Jon Harrop)
   2.  Re: Sorting arrays (Ahn, Ki Yung)
   3.  Re: Talking betwean the parsers. (Maciej Piechotka)
   4. Re:  Re: Talking betwean the parsers. (Felipe Lessa)
   5.  where scope (I. J. Kennedy)
   6. Re:  where scope (Isaac Dupree)
   7. Re:  where scope (Chadda? Fouch?)
   8.  QuickCheck help (Patrick LeBoutillier)
   9. Re:  QuickCheck help (Felipe Lessa)
  10.  dependent types (pat browne)


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

Message: 1
Date: Sat, 18 Jul 2009 13:13:25 +0100
From: Jon Harrop <[email protected]>
Subject: [Haskell-beginners] Sorting arrays
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="us-ascii"


I cannot find a sort function for arrays in the standard library. Is there 
one?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 2
Date: Sat, 18 Jul 2009 08:35:30 -0700
From: "Ahn, Ki Yung" <[email protected]>
Subject: [Haskell-beginners] Re: Sorting arrays
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Jon Harrop wrote:
> I cannot find a sort function for arrays in the standard library.
> Is there one?

No, there isn't.  They are not mutable arrays, so no point for writing 
an algorithm like sort.

There are several mutable array library implementations on Hackage, and 
some of them provide sorting algorithms in the library such as 
http://hackage.haskell.org/package/uvector-algorithms

--
   Ahn, Ki Yung



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

Message: 3
Date: Sat, 18 Jul 2009 22:51:26 +0000 (UTC)
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Talking betwean the parsers.
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Felipe Lessa <felipe.lessa <at> gmail.com> writes:

> 
> On Sat, Jul 18, 2009 at 12:40:15AM +0200, Maciej Piechotka wrote:
> > I have a data structure of
> > data Monad m => NntpConnection m = NntpConnection {
> >     input :: ByteString,
> >     output :: ByteString -> m ()
> >     }
> >
> > I'd like to create echo structure such that the goes to output is going
> > to (lazy) input. For sure it is possible to use network and IO monad -
> > is is possible to do it purely?
> 
> In words, not code: you may create a Chan of strict ByteStrings.
> On the output side you just append all the chunks of the lazy
> ByteString to the Chan, or you may copy the lazy ByteString in
> one chunk of strict ByteString.  On the input side you
> "getContents" and "fromChunks".  Should work, I guess.
> 
> --
> Felipe.
> 

Sorry - I don't follow:
1. How the output is suppose to get into input?
2. Why combine getContents and fromChunks if getContents is overloaded for
ByteString?
3. How to use getContents without IO - either network or posix pipe?

Regards




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

Message: 4
Date: Sat, 18 Jul 2009 23:34:54 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Re: Talking betwean the parsers.
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Jul 18, 2009 at 10:51:26PM +0000, Maciej Piechotka wrote:
] Felipe Lessa <felipe.lessa <at> gmail.com> writes:
] ] On Sat, Jul 18, 2009 at 12:40:15AM +0200, Maciej Piechotka wrote:
] ] ] I have a data structure of
] ] ] data Monad m => NntpConnection m = NntpConnection {
] ] ]   input :: ByteString,
] ] ]   output :: ByteString -> m ()
] ] ]     }
] ] ]
] ] ] I'd like to create echo structure such that the goes to output is going
] ] ] to (lazy) input. For sure it is possible to use network and IO monad -
] ] ] is is possible to do it purely?
] ]
] ] In words, not code: you may create a Chan of strict ByteStrings.
] ] On the output side you just append all the chunks of the lazy
] ] ByteString to the Chan, or you may copy the lazy ByteString in
] ] one chunk of strict ByteString.  On the input side you
] ] "getContents" and "fromChunks".  Should work, I guess.
] ]
] ] --
] ] Felipe.
] ]
]
] Sorry - I don't follow:
] 1. How the output is suppose to get into input?

Via the Chan.

] 2. Why combine getContents and fromChunks if getContents is overloaded for
] ByteString?

I said getContents but I meant getChanContents :).

] 3. How to use getContents without IO - either network or posix pipe?
 
It's not without the IO monad, but it works without doing I/O
(e.g. networking, files) and portably.

Ok, now with (literate Haskell) code :)

> import Control.Concurrent.Chan
> import qualified Data.ByteString.Lazy.Char8 as L
>
> data NntpConnection m = NntpConnection {
>       input :: L.ByteString,
>       output :: L.ByteString -> m ()
>     }
>
> echoConnIO :: IO (NntpConnection IO)
> echoConnIO = do
>   chan <- newChan
>   inputChunks <- getChanContents chan -- lazy!
>   return $ NntpConnection {
>     input  = L.fromChunks inputChunks,
>     output = mapM_ (writeChan chan) . L.toChunks }

*Main Control.Concurrent> c <- echoConnIO
*Main Control.Concurrent> forkIO $ L.putStrLn (input c)
ThreadId 65
*Main Control.Concurrent> output c $ L.pack "Hello, world!\n"
Hello, world!

Note that the input bytestring will never end because
NntpConnection doesn't have something like 'close :: m ()'.  If
you want something robust you'll probably move away from
representing you input as a lazy bytestring anyway.

--
Felipe.


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

Message: 5
Date: Mon, 20 Jul 2009 00:29:06 -0500
From: "I. J. Kennedy" <[email protected]>
Subject: [Haskell-beginners] where scope
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

GHC complains that  u  is out of scope in the following definition:

f n =
  let u = [1..n] in g n
  where
    g x = u

Why?  I understand that it's "just the way Haskell works", but I'm
wondering about the rationale of the language designers when deciding
on this scoping behavior.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090720/523710cb/attachment-0001.html

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

Message: 6
Date: Mon, 20 Jul 2009 02:14:17 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] where scope
To: "I. J. Kennedy" <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

I. J. Kennedy wrote:
> GHC complains that  u  is out of scope in the following definition:
> 
> f n =
>   let u = [1..n] in g n
>   where
>     g x = u
> 
> Why?  I understand that it's "just the way Haskell works", but I'm
> wondering about the rationale of the language designers when deciding
> on this scoping behavior.

the reason we need "where", is to scope over a function clause.  Do you 
know about guards?  like, here's a (very stupid) example where "let" 
doesn't work, so "where" scoping is needed:

f n
   | np == 0 = 1 + add
   | np == 1 = 3 + add
  where
    np = n + 1
    add = n + 2

-Isaac



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

Message: 7
Date: Mon, 20 Jul 2009 12:54:13 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] where scope
To: "I. J. Kennedy" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Mon, Jul 20, 2009 at 7:29 AM, I. J. Kennedy<[email protected]> wrote:
> GHC complains that  u  is out of scope in the following definition:
> f n =
>   let u = [1..n] in g n
>   where
>     g x = u
> Why?  I understand that it's "just the way Haskell works", but I'm
> wondering about the rationale of the language designers when deciding
> on this scoping behavior.

"let ... in" and "where" have fundamentally different scoping
behaviour : "where" is attached to a function definition clause (a
pattern matching, eventually several guards), "let ... in ..." is an
expression like any other.
"expression where ..." isn't valid Haskell, only "function clause where ..." is.

You should see your code as :
> (f n = let u = [1..n] in g n)
>   where g x = u

Not as :
> f n = (let u = [1..n] in g n) where g x = u

And certainly not as :
> f n = let u = [1..n] in (g n where g x = u)
which was probably the view you had initially that brought you to
think that "u" would be in scope.

In other words, "let" and "where" are not the interchangeable syntax
for which beginners often take them, they're not just a matter of
taste, each has its own usages that the other can't properly emulate.

-- 
Jedaï


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

Message: 8
Date: Mon, 20 Jul 2009 10:01:54 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] QuickCheck help
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I'm writing a small library to manipulate IPv4 addresses and masks.
The core types are as such:

data IPv4Host = IPv4Host { hbytes :: Word32 }
                deriving (Eq)
data IPv4Mask = IPv4Mask { mbytes :: Word32 }
                deriving (Eq)


I'm trying to test my library using QuickCheck. My test script looks
like this so far:

import Test.QuickCheck.Batch
import Test.QuickCheck

import Net.IPv4

prop_host_read_show :: Int -> Bool
prop_host_read_show i =
  let w = fromIntegral i
      h = IPv4Host w
  in read . show $ h == h

options = TestOptions { no_of_tests = 100 , length_of_tests = 0 ,
debug_tests = True }

main = do
    runTests "IPv4Host" options
        [ run prop_host_read_show
        ]


I'm generating arbitrary Ints that are then converted to Word32
values. The problem is that for 100 tests, all the generated Ints seem
to be
below +/- 100.

How do I get the Ints to be more evenly distributed over the entire 32
bit range? I tried making Word32 and instance if Arbitrary using
choose (0, 0xFFFFFFFF) but I got the same results...


Thanks,

Patrick


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


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

Message: 9
Date: Mon, 20 Jul 2009 11:33:44 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] QuickCheck help
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, Jul 20, 2009 at 10:01:54AM -0400, Patrick LeBoutillier wrote:
> prop_host_read_show :: Int -> Bool
> prop_host_read_show i =
>   let w = fromIntegral i
>       h = IPv4Host w
>   in read . show $ h == h

The last line is equivalent to "in read (show (h == h))", so you
are testing if your Eq instance is an Eq instance and if "read
(show True)" really is True. :)

You probably want "(read $ show h) == h".

--
Felipe.


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

Message: 10
Date: Mon, 20 Jul 2009 17:50:53 +0100
From: pat browne <[email protected]>
Subject: [Haskell-beginners] dependent types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

{- Hi,
Consider the following Haskell data, class and instance definitions
The intended semantics are:
    Two objects are equal if they have the same name.
    The type of the name depends on the type of the object
-}

data Object a  = Object a

class Named o n | o -> n where
            name :: o  -> n


instance (Eq n, Named o n) => Eq o where
                o1 == o2 = name(o1) == name(o2)
{-
I have three questions about these definitions.
1) Does the above code capture the intended meaning?
2) Does   Object a  *fit* the Named class?,
   or do I need a different sort of Object?
3) How would I write an instance of Named that implements the name
function correctly. Thomson[1] shows how to do this for non-dependent types

[1]Haskell: The Craft of Functional Programming, Second Edition, Page 272
-}



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

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


End of Beginners Digest, Vol 13, Issue 10
*****************************************

Reply via email to