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: list of integers to list of nats (Christian Maeder)
   2.  Re: list of integers to list of nats (Christian Maeder)
   3.  Re: Decent Haskell REPL? (Christian Maeder)
   4.  Re: Decent Haskell REPL? (Christian Maeder)
   5. Re:  Translating imperative algorithms to Haskell (Tom Tobin)
   6. Re:  Decent Haskell REPL? (Tom Tobin)
   7.  Moment of enlightenment for lazy evaluation (Tom Tobin)
   8. Re:  list of integers to list of nats ([email protected])
   9. Re:  Decent Haskell REPL? (Jon Harrop)
  10. Re:  Decent Haskell REPL? (Tom Tobin)
  11. Re:  Moment of enlightenment for lazy evaluation
      (Stephen Blackheath [to Haskell-Beginners])


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

Message: 1
Date: Sat, 20 Feb 2010 15:59:52 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: list of integers to list of nats
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

[email protected] schrieb:
> 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))]

Since the result type is "Maybe [Nat]" the result should be:
  Just [S (S Z), Z, S Z, S (S (S Z))]

I suppose the result should be Nothing if the input list contains any
negative integer.

> how can I do that?

Who is your instructor? The most basic approach is do pattern-match on
the input list.

    mapIntsToNats l = case l of
       [] -> ...
       h : t -> ...

and call "mapIntsToNats" recursively on the tail "t". The recursive
result is (also) of type "Maybe [Nat]", therefore you have to further
pattern-match this result and insert the converted head element "h".

Is this enough to help you?
C.



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

Message: 2
Date: Sat, 20 Feb 2010 16:21:55 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: list of integers to list of nats
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

[email protected] schrieb:
> 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))

this function has type "Int -> Nat" and should be wrapped into a
function of type "Int -> Maybe Nat" that does not produce a runtime
error if called with negative input.

> 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?

Have you heard about monads and mapM? (The type constructor Maybe is an
instance of the class Monad.)

Cheers Christian


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

Message: 3
Date: Sat, 20 Feb 2010 16:28:27 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: Decent Haskell REPL?
To: Jon Harrop <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Jon Harrop schrieb:
> On Friday 19 February 2010 18:46:30 Tom Tobin wrote:
>> On Fri, Feb 19, 2010 at 1:57 PM, Jon Harrop <[email protected]> wrote:
>>> GHCI seems to require type definitions to be described in a separate file
>>> and then loaded into the REPL, which is seriously tedious.
>>>
>>> Are there any Haskell REPLs that don't have such limitations and,
>>> preferably, use JIT compilation instead of an interpreter?
>> I run Haskell in an inferior shell in Emacs alongside haskell-mode;
>> it's easy to get immediate feedback to my changes that way.
> 
> What's your procedure for getting ghci to interpret a type definition?
> 

Typing ":r" to reload the currently loaded but later changed or extended
file.

C.



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

Message: 4
Date: Sat, 20 Feb 2010 16:31:32 +0100
From: Christian Maeder <[email protected]>
Subject: [Haskell-beginners] Re: Decent Haskell REPL?
To: Jon Harrop <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Jon Harrop schrieb:
> On Friday 19 February 2010 18:46:30 Tom Tobin wrote:
>> On Fri, Feb 19, 2010 at 1:57 PM, Jon Harrop <[email protected]> wrote:
>>> GHCI seems to require type definitions to be described in a separate file
>>> and then loaded into the REPL, which is seriously tedious.
>>>
>>> Are there any Haskell REPLs that don't have such limitations and,
>>> preferably, use JIT compilation instead of an interpreter?
>> I run Haskell in an inferior shell in Emacs alongside haskell-mode;
>> it's easy to get immediate feedback to my changes that way.
> 
> What's your procedure for getting ghci to interpret a type definition?

In other words: it does not work without a file to introduce types, but
I don't think that's a problem.

C.


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

Message: 5
Date: Sat, 20 Feb 2010 10:19:28 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Feb 19, 2010 at 4:17 PM, Tom Tobin <[email protected]> wrote:
> It's like asking to express a square in terms of a circle -- you can
> come up with a circle that has the same area

Heh, it was pointed out to me that you actually *can't* do this:

http://en.wikipedia.org/wiki/Squaring_the_circle

You can get a circle that has *approximately* the same area, but never
exactly.  Curse my lack of math background!  ^_^


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

Message: 6
Date: Sat, 20 Feb 2010 10:33:30 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] Decent Haskell REPL?
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Sat, Feb 20, 2010 at 9:31 AM, Jon Harrop <[email protected]> wrote:
> On Friday 19 February 2010 18:46:30 Tom Tobin wrote:
>> On Fri, Feb 19, 2010 at 1:57 PM, Jon Harrop <[email protected]> wrote:
>> > GHCI seems to require type definitions to be described in a separate file
>> > and then loaded into the REPL, which is seriously tedious.
>> >
>> > Are there any Haskell REPLs that don't have such limitations and,
>> > preferably, use JIT compilation instead of an interpreter?
>>
>> I run Haskell in an inferior shell in Emacs alongside haskell-mode;
>> it's easy to get immediate feedback to my changes that way.
>
> What's your procedure for getting ghci to interpret a type definition?

I just hit C-c C-r in my Haskell source file's buffer to get it
reloaded in the inferior ghci shell.

I think it's best to think of the source file as your scratchpad for
editing, with the "main" function changed as you go along to show
relevant output for testing, and the inferior ghci buffer as just an
"output window" of sorts.  It's not the same as writing code directly
shell, but I actually prefer it this way for all languages now, not
just Haskell -- particularly since it's much nicer editing code in a
proper editor vs. in a shell.


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

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

Instead of a question, I thought I'd share a moment of lazy-evaluation
enlightenment I had last night.

I have some code that recursively descends a directory, gets the SHA1
hashes for all the files, and builds a map of which file paths share
the same SHA1 hash.  The code that actually generates the hash looked
like this:

sha1file :: FilePath -> IO String
sha1file fn = do
  bs <- expandPath fn >>= BSL.readFile
  return $ PureSHA.showDigest $ PureSHA.sha1 bs

Everything worked fine on paths without many files in them, but choked
on paths with many files:

"Exception: getCurrentDirectory: resource exhausted (Too many open files)"

This was driving me crazy; ByteString.Lazy.readFile is supposed to
close the file once it's done.  I kept going over my code, wondering
what was at fault, until it finally clicked: *the hashes weren't being
generated until I actually tried to view them*, and thus all the files
were being held open until that point!  I made a single change to my
"sha1file" function:

sha1file :: FilePath -> IO String
sha1file fn = do
  bs <- expandPath fn >>= BSL.readFile
  return $ PureSHA.showDigest $! PureSHA.sha1 bs

(the "$!") ... and everything worked perfectly.  The code now finished
processing each file before opening the next one, and I was happy.
:-)


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

Message: 8
Date: Sat, 20 Feb 2010 19:18:57 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] list of integers to list of nats
To: <[email protected]> <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

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


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

Message: 9
Date: Sat, 20 Feb 2010 19:44:30 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Decent Haskell REPL?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Saturday 20 February 2010 16:33:30 Tom Tobin wrote:
> I just hit C-c C-r in my Haskell source file's buffer to get it
> reloaded in the inferior ghci shell.
>
> I think it's best to think of the source file as your scratchpad for
> editing, with the "main" function changed as you go along to show
> relevant output for testing, and the inferior ghci buffer as just an
> "output window" of sorts.  It's not the same as writing code directly
> shell, but I actually prefer it this way for all languages now, not
> just Haskell -- particularly since it's much nicer editing code in a
> proper editor vs. in a shell.

I see. What happens if your program is producing non-trivial output that takes 
a long time to compute? How do you get around complete recomputation at the 
evaluation of every new expression?

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


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

Message: 10
Date: Sat, 20 Feb 2010 13:08:52 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] Decent Haskell REPL?
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Sat, Feb 20, 2010 at 1:44 PM, Jon Harrop <[email protected]> wrote:
> On Saturday 20 February 2010 16:33:30 Tom Tobin wrote:
>> I just hit C-c C-r in my Haskell source file's buffer to get it
>> reloaded in the inferior ghci shell.
>>
>> I think it's best to think of the source file as your scratchpad for
>> editing, with the "main" function changed as you go along to show
>> relevant output for testing, and the inferior ghci buffer as just an
>> "output window" of sorts.  It's not the same as writing code directly
>> shell, but I actually prefer it this way for all languages now, not
>> just Haskell -- particularly since it's much nicer editing code in a
>> proper editor vs. in a shell.
>
> I see. What happens if your program is producing non-trivial output that takes
> a long time to compute? How do you get around complete recomputation at the
> evaluation of every new expression?

Hm, I'm not sure.  I do seem to lose any local "let" bindings I do in
the ghci window when I reload, and the manual for ghci doesn't seem to
offer any way to save values between reloads [1].  I know you can
avoid that in other languages' shells (e.g., Python).  One workaround
I can think of is to use the "binary" package to save intermediate
computations out to disk, but that seems somewhat cumbersome when
you're trying to quickly prototype your code.

[1] 
http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html


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

Message: 11
Date: Sun, 21 Feb 2010 11:42:37 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] Moment of enlightenment for lazy
        evaluation
To: Tom Tobin <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Tom,

The bad news is that 1. Haskell makes no guarantee about when the files
are closed, 2. file handles are a limited resource, and 3. lazy I/O
doesn't handle errors in a recoverable fashion.  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.


Steve

Tom Tobin wrote:
> Instead of a question, I thought I'd share a moment of lazy-evaluation
> enlightenment I had last night.
> 
> I have some code that recursively descends a directory, gets the SHA1
> hashes for all the files, and builds a map of which file paths share
> the same SHA1 hash.  The code that actually generates the hash looked
> like this:
> 
> sha1file :: FilePath -> IO String
> sha1file fn = do
>   bs <- expandPath fn >>= BSL.readFile
>   return $ PureSHA.showDigest $ PureSHA.sha1 bs
> 
> Everything worked fine on paths without many files in them, but choked
> on paths with many files:
> 
> "Exception: getCurrentDirectory: resource exhausted (Too many open files)"
> 
> This was driving me crazy; ByteString.Lazy.readFile is supposed to
> close the file once it's done.  I kept going over my code, wondering
> what was at fault, until it finally clicked: *the hashes weren't being
> generated until I actually tried to view them*, and thus all the files
> were being held open until that point!  I made a single change to my
> "sha1file" function:
> 
> sha1file :: FilePath -> IO String
> sha1file fn = do
>   bs <- expandPath fn >>= BSL.readFile
>   return $ PureSHA.showDigest $! PureSHA.sha1 bs
> 
> (the "$!") ... and everything worked perfectly.  The code now finished
> processing each file before opening the next one, and I was happy.
> :-)
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
> 


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

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


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

Reply via email to