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:  Moment of enlightenment for lazy evaluation (Tom Tobin)
   2. Re:  Moment of enlightenment for lazy evaluation (Daniel Fischer)
   3.  help with error (Tyler Hayes)
   4. Re:  help with error (Daniel Fischer)
   5. Re:  help with error (Tyler Hayes)
   6. Re:  Translating imperative algorithms to Haskell (Stephen Tetley)
   7. Re:  list of integers to list of nats ([email protected])
   8. Re:  list of integers to list of nats (Daniel Fischer)
   9. Re:  list of integers to list of nats ([email protected])


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

Message: 1
Date: Sat, 20 Feb 2010 19:23:48 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] Moment of enlightenment for lazy
        evaluation
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Sat, Feb 20, 2010 at 4:42 PM, Stephen Blackheath [to
Haskell-Beginners] <[email protected]>
wrote:
> Tom,
>
> The bad news is that 1. Haskell makes no guarantee about when the files
> are closed,

Hmm, Data.ByteString.Lazy.readFile's docstring says:

"Read an entire file lazily into a ByteString. The Handle will be held
open until EOF is encountered."

It certainly seemed to change matters once I switched that $ to $!,
though; I don't see why that would have helped me unless the handles
were indeed being closed.

> 2. file handles are a limited resource

Well, yes, that's why I ran into the original problem.

> and 3. lazy I/O
> doesn't handle errors in a recoverable fashion.

I suppose this will be something I'll run into before too long.

> Unfortunately this
> means that lazy I/O is fundamentally unsound.
>
> The only safe way to do it is to read the file strictly in blocks using
> Data.ByteString.hGet.

But with the strict version of ByteString, how would I compute the
SHA1 hash of an 8 GB file on a machine with quite a bit less memory?
I can't imagine Haskell just has no way to handle a case that other
languages handle easily.


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

Message: 2
Date: Sun, 21 Feb 2010 02:57:11 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Moment of enlightenment for lazy
        evaluation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Sonntag 21 Februar 2010 02:23:48 schrieb Tom Tobin:
> On Sat, Feb 20, 2010 at 4:42 PM, Stephen Blackheath [to
> Haskell-Beginners] <[email protected]>
>
> wrote:
> > Tom,
> >
> > The bad news is that 1. Haskell makes no guarantee about when the
> > files are closed,
>
> Hmm, Data.ByteString.Lazy.readFile's docstring says:
>
> "Read an entire file lazily into a ByteString. The Handle will be held
> open until EOF is encountered."

There is no hard guarantee when the file will be closed, but looking at the 
relevant code,

hGetContentsN :: Int -> Handle -> IO ByteString
hGetContentsN k h = lazyRead -- TODO close on exceptions
  where
    lazyRead = unsafeInterleaveIO loop

    loop = do
        c <- S.hGetNonBlocking h k
        --TODO: I think this should distinguish EOF from no data available
        -- the underlying POSIX call makes this distincion, returning 
either
        -- 0 or EAGAIN
        if S.null c
          then do eof <- hIsEOF h
                  if eof then hClose h >> return Empty
                         else hWaitForInput h (-1)
                           >> loop
          else do cs <- lazyRead
                  return (Chunk c cs)

I'd say the file is closed as soon as EOF is encountered. If you don't open 
too many files before you've finished reading, it shouldn't be a problem.

>
> It certainly seemed to change matters once I switched that $ to $!,
> though; I don't see why that would have helped me unless the handles
> were indeed being closed.
>

Right. The $! forced the file to be read until the end, so it was closed 
before too many others were opened.

> > 2. file handles are a limited resource
>
> Well, yes, that's why I ran into the original problem.
>
> > and 3. lazy I/O
> > doesn't handle errors in a recoverable fashion.
>
> I suppose this will be something I'll run into before too long.
>
> > Unfortunately this
> > means that lazy I/O is fundamentally unsound.
> >
> > The only safe way to do it is to read the file strictly in blocks
> > using Data.ByteString.hGet.
>
> But with the strict version of ByteString, how would I compute the
> SHA1 hash of an 8 GB file on a machine with quite a bit less memory?
> I can't imagine Haskell just has no way to handle a case that other
> languages handle easily.

Incrementally, like the SHA1 hash is computed with a lazy ByteString.
Read a chunk of the file (multiple of 512 bits is a good idea), process it, 
read next chunk, ..., until the end, then close the file.

The difference is that you have exact control what happens when this way, 
the unsafeInterleaveIO in the lazy ByteString code takes that control away.
However, by forcing the results at the proper places, you gain enough 
control to avoid the leaking of file handles and several other unpleasant 
surprises - normally, at least, there may be cases where you can't.



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

Message: 3
Date: Sat, 20 Feb 2010 19:07:50 -0800
From: Tyler Hayes <[email protected]>
Subject: [Haskell-beginners] help with error
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain

I'm getting an error and I do not know why...

This is what I have:


class Finite a where
  elements' :: [a]

instance (Finite a, Show a, Show b) => Show (a -> b) where
   show f = concat (map show [ a | a <- elements'::[a] ])


and this is what ghci gives me:


    Could not deduce (Finite a1) from the context ()
      arising from a use of `elements'' at Hw06.lhs:119:42-50
    Possible fix:
      add (Finite a1) to the context of an expression type signature
    In the expression: elements' :: [a]
    In a stmt of a list comprehension: a <- elements' :: [a]
    In the second argument of `map', namely
        `[a | a <- elements' :: [a]]'


What's going on here?

Thanks for the help!
- Tyler



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

Message: 4
Date: Sun, 21 Feb 2010 04:36:32 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] help with error
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Sonntag 21 Februar 2010 04:07:50 schrieb Tyler Hayes:
> I'm getting an error and I do not know why...
>
> This is what I have:
>
>
> class Finite a where
>   elements' :: [a]
>
> instance (Finite a, Show a, Show b) => Show (a -> b) where
>    show f = concat (map show [ a | a <- elements'::[a] ])
>
>
> and this is what ghci gives me:
>
>
>     Could not deduce (Finite a1) from the context ()
>       arising from a use of `elements'' at Hw06.lhs:119:42-50
>     Possible fix:
>       add (Finite a1) to the context of an expression type signature
>     In the expression: elements' :: [a]
>     In a stmt of a list comprehension: a <- elements' :: [a]
>     In the second argument of `map', namely
>         `[a | a <- elements' :: [a]]'
>
>
> What's going on here?

All type variables are implicitly universally quantified. Thus the type 
variable a in [a | a <- elements' :: [a] ] is *not* the type variable a 
from the signature, it's a fresh type variable (and promises something you 
can't keep, that elements' belongs to *every* list type).

In GHC, you can bring the type variables from the signature into scope by 
enabling ScopedTypeVariables and explicitly quantifying the type variables:

{-# LANGUAGE ScopedTypeVariables #-}
-- or -XScopedTypeVariables on the command line

class Finite a where
    elements' :: [a]

instance forall a b. (Finite a, Show a, Show b) => Show (a -> b) where
    show f = concatMap show (elements' :: [a])

But that isn't really a good show instance, it involves neither b nor f, 
perhaps you'd rather want

instance (Finite a, Show a, Show b) => Show (a -> b) where
    show f = show [ (x, f x) | x <- elements' ]

and then you don't need ScopedTypeVariables and explicit forall.

>
> Thanks for the help!
> - Tyler



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

Message: 5
Date: Sat, 20 Feb 2010 20:04:46 -0800
From: Tyler Hayes <[email protected]>
Subject: Re: [Haskell-beginners] help with error
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain

Thanks that helps a lot!

- Tyler

On Sun, 2010-02-21 at 04:36 +0100, Daniel Fischer wrote:
> Am Sonntag 21 Februar 2010 04:07:50 schrieb Tyler Hayes:
> > I'm getting an error and I do not know why...
> >
> > This is what I have:
> >
> >
> > class Finite a where
> >   elements' :: [a]
> >
> > instance (Finite a, Show a, Show b) => Show (a -> b) where
> >    show f = concat (map show [ a | a <- elements'::[a] ])
> >
> >
> > and this is what ghci gives me:
> >
> >
> >     Could not deduce (Finite a1) from the context ()
> >       arising from a use of `elements'' at Hw06.lhs:119:42-50
> >     Possible fix:
> >       add (Finite a1) to the context of an expression type signature
> >     In the expression: elements' :: [a]
> >     In a stmt of a list comprehension: a <- elements' :: [a]
> >     In the second argument of `map', namely
> >         `[a | a <- elements' :: [a]]'
> >
> >
> > What's going on here?
> 
> All type variables are implicitly universally quantified. Thus the type 
> variable a in [a | a <- elements' :: [a] ] is *not* the type variable a 
> from the signature, it's a fresh type variable (and promises something you 
> can't keep, that elements' belongs to *every* list type).
> 
> In GHC, you can bring the type variables from the signature into scope by 
> enabling ScopedTypeVariables and explicitly quantifying the type variables:
> 
> {-# LANGUAGE ScopedTypeVariables #-}
> -- or -XScopedTypeVariables on the command line
> 
> class Finite a where
>     elements' :: [a]
> 
> instance forall a b. (Finite a, Show a, Show b) => Show (a -> b) where
>     show f = concatMap show (elements' :: [a])
> 
> But that isn't really a good show instance, it involves neither b nor f, 
> perhaps you'd rather want
> 
> instance (Finite a, Show a, Show b) => Show (a -> b) where
>     show f = show [ (x, f x) | x <- elements' ]
> 
> and then you don't need ScopedTypeVariables and explicit forall.
> 
> >
> > Thanks for the help!
> > - Tyler
> 
> 



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

Message: 6
Date: Sun, 21 Feb 2010 12:31:59 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello all

How are search trees generated and what is their 'shape' - i.e. leaf
labelled, node labelled, binary trees or rose trees?

I've a functional reformulation of the Wikipedia algorithm which is
about the same line count (excepting auxiliaries, which is a bit of a
cheat), but its producing bad results on a leaf and node labelled rose
tree.

By the way, the imperative essence of the negascout algorithm and what
makes it elegant is how it cuts off (control flow), rather than
statefulness (assignment). Even though the line count is roughly the
same and I believe I match the traversal behaviour / cut offs, the
imperative version is simply nicer than my functional version.

Best wishes

Stephen


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

Message: 7
Date: Sun, 21 Feb 2010 15:57:09 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] list of integers to list of nats
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I tried: map toEnum [1,2,3,4]) :: [Nat]
which works normally, but not for Maybe in: mapIntsToNats :: [Int] -> Maybe 
[Nat]


-------- Original-Nachricht --------
> Datum: Sat, 20 Feb 2010 19:18:57 +0100
> Von: [email protected]
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] list of integers to list of nats

> the function should produce Nothing if there is a negative Int in the
> input list and otherwise Just with the list of the corresponding Nats
> 
> mapM looks like the right function for that, so I tried some examples that
> work like I need it. But in case of my exercise: 
> 
> mapM toEnum [1,2,3,4] :: Nat
> 
> doesn't work.
> 
> 
> -------- Original-Nachricht --------
> > Datum: Sat, 20 Feb 2010 13:19:32 +0100
> > Von: "Jonas Almström Duregård" <[email protected]>
> > An: [email protected]
> > Betreff: Re: [Haskell-beginners] list of integers to list of nats
> 
> > Homework?
> > 
> > Should mapIntsToNats [-1] be Nothing?
> > 
> > One way to to do this is to check if any integer in the list is
> > negative (for instance using the any function) and if so return
> > Nothing, otherwise Just map an Int-to-Nat function across the values
> > in the list (there is already such a function in your code).
> > 
> > A slightly more elegant solution uses the fact that Maybe is a Monad,
> > so the function
> > 
> > mapM  :: Monad m => (a -> m b) -> [a] -> m [b]
> > 
> > is also mapM :: (Int -> Maybe Nat) -> [Int] -> Maybe [Nat].
> > 
> > so mapIntsToNats = mapM  f, for some function f :: Int -> Maybe Nat
> > 
> > Hope this helps
> > /Jonas Duregård
> > 
> > 
> > On 20 February 2010 12:39,  <[email protected]> wrote:
> > > Hi,
> > > I have to write a function which maps a list of integers to a list of
> > the corresponding nats. The following code is already there:
> > >
> > > data Nat = Z | S Nat deriving (Eq,Ord,Show)
> > >
> > > instance Enum Nat where
> > >    toEnum i | i < 0        = error "Enum_Nat.toEnum: Negative
> > integer"
> > >             | i == 0       = Z
> > >             | otherwise    = S (toEnum (i-1))
> > >
> > > the function should be: mapIntsToNats :: [Int] -> Maybe [Nat]
> > > so for example: [2,0,1,3] should make: [S (S Z), Z, S Z, S (S (S Z))]
> > >
> > > how can I do that?
> > >
> > >
> > >
> > > --
> > > NEU: Mit GMX DSL über 1000,- ¿ sparen!
> > > http://portal.gmx.net/de/go/dsl02
> > > _______________________________________________
> > > Beginners mailing list
> > > [email protected]
> > > http://www.haskell.org/mailman/listinfo/beginners
> > >
> 
> -- 
> NEU: Mit GMX DSL über 1000,- ¿ sparen!
> http://portal.gmx.net/de/go/dsl02
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


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

Message: 8
Date: Sun, 21 Feb 2010 16:28:58 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] list of integers to list of nats
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Sonntag 21 Februar 2010 15:57:09 schrieb [email protected]:
> I tried: map toEnum [1,2,3,4]) :: [Nat]
> which works normally, but not for Maybe in: mapIntsToNats :: [Int] ->
> Maybe [Nat]
>

To use mapM, you need a function of type (f :: Int -> Maybe Nat). toEnum 
has type (Int -> Nat), so you can't use that directly.
Think: when should f k be Nothing and what should f k be in the other 
cases?


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

Message: 9
Date: Sun, 21 Feb 2010 17:28:41 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] list of integers to list of nats
To: Daniel Fischer <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

f should be Nothing if k is negative and otherwise k should be toEnum k...?!


-------- Original-Nachricht --------
> Datum: Sun, 21 Feb 2010 16:28:58 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] list of integers to list of nats

> Am Sonntag 21 Februar 2010 15:57:09 schrieb [email protected]:
> > I tried: map toEnum [1,2,3,4]) :: [Nat]
> > which works normally, but not for Maybe in: mapIntsToNats :: [Int] ->
> > Maybe [Nat]
> >
> 
> To use mapM, you need a function of type (f :: Int -> Maybe Nat). toEnum 
> has type (Int -> Nat), so you can't use that directly.
> Think: when should f k be Nothing and what should f k be in the other 
> cases?

-- 
Sicherer, schneller und einfacher. Die aktuellen Internet-Browser -
jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/atbrowser


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

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


End of Beginners Digest, Vol 20, Issue 33
*****************************************

Reply via email to