Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Renato Leal has invited you to Dropbox (Dropbox) 2. sqrt root issues (Bryce Verdier) 3. Re: sqrt root issues (Sean Perry) 4. Re: sqrt root issues (Daniel Fischer) 5. More Deserialization Woes (Tom Hobbs) 6. Re: More Deserialization Woes (Benjamin Edwards) ---------------------------------------------------------------------- Message: 1 Date: Wed, 30 Jun 2010 19:03:07 +0000 From: Dropbox <no-re...@dropbox.com> Subject: [Haskell-beginners] Renato Leal has invited you to Dropbox To: beginners@haskell.org Message-ID: <20100630190307.e690d307...@mailman.dropbox.com> Content-Type: text/plain; charset="utf8" We're excited to let you know that Renato Leal has invited you to Dropbox! Renato Leal has been using Dropbox to sync and share files online and across computers, and thought you might want it too. Visit http://www.dropbox.com/link/20.R3oEQnNe2Y/NjIzNjAwNTgxNw to get started. - The Dropbox Team ____________________________________________________ To stop receiving invites from Dropbox, please go to http://www.dropbox.com/bl/e9a1876312ac/beginners%40haskell.org -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20100630/9128cbde/attachment-0001.html ------------------------------ Message: 2 Date: Wed, 30 Jun 2010 20:01:41 -0700 From: Bryce Verdier <bryceverd...@gmail.com> Subject: [Haskell-beginners] sqrt root issues To: haskellbeginners <beginners@haskell.org> Message-ID: <4c2c0515.8010...@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hi all, I'm having a real hard time wrapping my head around a problem; Here is a basic, no frills, function. prime divisor divided | divided == 1 = True | divisor== divided= True | mod divided divisor== 0 = False | otherwise = next_prime where next_prime = prime (divisor + 2) divided and the calling function: is_prime input = prime 3 input I'm trying to get the square root of input as an Integer. In GHCI: :t (toInteger $ round $ sqrt 25) (toInteger $ round $ sqrt 25) :: Integer but if I change is_prime input = prime 3 (toInteger $ round $ sqrt input) I get this error: problem3.hs:19:33: No instance for (RealFrac Integer) arising from a use of `round' at problem3.hs:19:33-37 Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `($)', namely `round' In the second argument of `($)', namely `round $ sqrt input' In the expression: toInteger $ round $ sqrt input problem3.hs:19:41: No instance for (Floating Integer) arising from a use of `sqrt' at problem3.hs:19:41-50 Possible fix: add an instance declaration for (Floating Integer) In the second argument of `($)', namely `sqrt input' In the second argument of `($)', namely `round $ sqrt input' In the expression: toInteger $ round $ sqrt input Failed, modules loaded: none. Can someone please help me out and tell me what I'm missing? Thanks in advance, Bryce ------------------------------ Message: 3 Date: Wed, 30 Jun 2010 21:08:08 -0700 From: Sean Perry <sha...@speakeasy.net> Subject: Re: [Haskell-beginners] sqrt root issues To: Bryce Verdier <bryceverd...@gmail.com> Cc: haskellbeginners <beginners@haskell.org> Message-ID: <1277957288.4698.125.ca...@turion> Content-Type: text/plain On Wed, 2010-06-30 at 20:01 -0700, Bryce Verdier wrote: > Hi all, > > I'm having a real hard time wrapping my head around a problem; > > Here is a basic, no frills, function. > prime divisor divided > | divided == 1 = True > | divisor== divided= True > | mod divided divisor== 0 = False > | otherwise = next_prime > where next_prime = prime (divisor + 2) divided > > and the calling function: > is_prime input = prime 3 input > > I'm trying to get the square root of input as an Integer. > > In GHCI: > :t (toInteger $ round $ sqrt 25) > (toInteger $ round $ sqrt 25) :: Integer > > but if I change is_prime input = prime 3 (toInteger $ round $ sqrt input) > > I get this error: > problem3.hs:19:33: > No instance for (RealFrac Integer) > arising from a use of `round' at problem3.hs:19:33-37 > Possible fix: add an instance declaration for (RealFrac Integer) > In the first argument of `($)', namely `round' > In the second argument of `($)', namely `round $ sqrt input' > In the expression: toInteger $ round $ sqrt input > > problem3.hs:19:41: > No instance for (Floating Integer) > arising from a use of `sqrt' at problem3.hs:19:41-50 > Possible fix: add an instance declaration for (Floating Integer) > In the second argument of `($)', namely `sqrt input' > In the second argument of `($)', namely `round $ sqrt input' > In the expression: toInteger $ round $ sqrt input > Failed, modules loaded: none. > > Can someone please help me out and tell me what I'm missing? > Prelude> let x = 25 Prelude> toInteger $ round $ sqrt x <interactive>:1:12: No instance for (RealFrac Integer) arising from a use of `round' at <interactive>:1:12-16 Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of `($)', namely `round' In the second argument of `($)', namely `round $ sqrt x' In the expression: toInteger $ round $ sqrt x <interactive>:1:20: No instance for (Floating Integer) arising from a use of `sqrt' at <interactive>:1:20-25 Possible fix: add an instance declaration for (Floating Integer) In the second argument of `($)', namely `sqrt x' In the second argument of `($)', namely `round $ sqrt x' In the expression: toInteger $ round $ sqrt x Prelude> :t x x :: Integer Prelude> :t 25 25 :: (Num t) => t Left to its own devices ghci chooses "Integer" for the type of x but the constant 25 is the more generic "Num". This is the source of your problems. is_prime2 :: (Floating a, RealFrac a) => a -> Bool is_prime2 input = prime 3 (toInteger $ round $ sqrt input) satisfies the compilation. But do you really need the call to toInteger? The round() call by itself appears to be sufficient. If you just want round then you do not need the type specifier at all. You should look more closely at your definition of prime though. It is easy to give it numbers that has it recursing for ever. ------------------------------ Message: 4 Date: Thu, 1 Jul 2010 11:49:23 +0200 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] sqrt root issues To: beginners@haskell.org Message-ID: <201007011149.23759.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-1" On Thursday 01 July 2010 05:01:41, Bryce Verdier wrote: > Hi all, > > I'm having a real hard time wrapping my head around a problem; > > Here is a basic, no frills, function. > prime divisor divided > > | divided == 1 = True > | divisor== divided= True > | mod divided divisor== 0 = False > | otherwise = next_prime > > where next_prime = prime (divisor + 2) divided > > and the calling function: > is_prime input = prime 3 input > > I'm trying to get the square root of input as an Integer. > > In GHCI: > :t (toInteger $ round $ sqrt 25) > > (toInteger $ round $ sqrt 25) :: Integer > > but if I change is_prime input = prime 3 (toInteger $ round $ sqrt > input) > > I get this error: > problem3.hs:19:33: > No instance for (RealFrac Integer) > arising from a use of `round' at problem3.hs:19:33-37 > Possible fix: add an instance declaration for (RealFrac Integer) > In the first argument of `($)', namely `round' > In the second argument of `($)', namely `round $ sqrt input' > In the expression: toInteger $ round $ sqrt input > > problem3.hs:19:41: > No instance for (Floating Integer) > arising from a use of `sqrt' at problem3.hs:19:41-50 > Possible fix: add an instance declaration for (Floating Integer) > In the second argument of `($)', namely `sqrt input' > In the second argument of `($)', namely `round $ sqrt input' > In the expression: toInteger $ round $ sqrt input > Failed, modules loaded: none. > > Can someone please help me out and tell me what I'm missing? The call to toInteger is unnecessary, round will return an Integer if you want one. What you are missing is a call to fromInetger or fromIntegral. sqrt takes an argument whose type belongs to the class Floating. The divisibilty test (mod) requires the type of input to be a member of Integral. There are no standard types which belong to both classes (and it wouldn't really make sense to make a type an instance of Integral and Floating). So you have to convert input to a type acceptable for sqrt. is_prime input = prime 3 (round . sqrt . fromIntegral $ input) With an integer-literal it works, because an integer literal n stands for (fromInteger n), so the integer is automatically converted to whatever number type is needed. > > Thanks in advance, > > Bryce ------------------------------ Message: 5 Date: Thu, 1 Jul 2010 11:55:47 +0100 From: Tom Hobbs <tvho...@googlemail.com> Subject: [Haskell-beginners] More Deserialization Woes To: beginners@haskell.org Message-ID: <aanlktim3x_a9wlokdc-0hihd-qnklrnv1kcmc-mfa...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Hi guys, Thanks for all the previous help I'm having with serialization in Haskell, I couldn't have gotten as far as I have without the help from this group. Many thanks! Now I've put you in a good mood, maybe you can help me with my latest problem... :-) I have the following code which reads strings from a handle; import qualified Data.ByteString.Lazy.UTF8 as UTF readNames 0 _ = [] readNames n h = do length <- fmap (fromIntegral . runGet getWord32be) $ L.hGet h 4 name <- L.hGet h length (UTF.toString name) : readNames (n-1) h Where n is the number of names remaining to read and h is the handle to the stream that I'm reading from. It is my intention that this function would return a [String] of all the names. I'm sure further explainations probably aren't necessary, but just in case; - "length" becomes equal to some number (represented by four bytes) which describes the length of the string to read - "name" becomes the string I'm trying to read - Then I add that String to the list and go again But the problem I'm getting is this; Couldn't match expected type `[a]' against inferred type `IO b' In a stmt of a 'do' expression: length <- fmap (fromIntegral . runGet getWord32be) $ L.hGet h 4 In the expression: do { length <- fmap (fromIntegral . runGet getWord32be) $ L.hGet h 4; name <- L.hGet h length; (UTF.toString name) : readNames (n - 1) h } In the definition of `readNames': readNames n h = do { length <- fmap (fromIntegral . runGet getWord32be) $ L.hGet h 4; name <- L.hGet h length; (UTF.toString name) : readNames (n - 1) h } I (think I) understand what the message is saying, but not why it's occurring. I read the documentation as "UTF8.tostring" essentially rips the [Char] out of the ByteString and returns that, so where does the "IO b" come into it? Is it complaining because the reads from the handle are IO operations and it's expecting me to use the IO monad somewhere? What i want to be able to do is do the IO stuff inside the monad, but end up with a [String] which I can then use in a pure function. RWH and Learn You A Good Haskell all cover some stream reading, but their examples are to different from what I'm trying to do that I'm getting stuck. Any pointers are very gratefully accepted. Many thanks, Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20100701/2c683ffb/attachment-0001.html ------------------------------ Message: 6 Date: Thu, 1 Jul 2010 13:03:39 +0200 From: Benjamin Edwards <edwards.b...@gmail.com> Subject: Re: [Haskell-beginners] More Deserialization Woes To: Tom Hobbs <tvho...@googlemail.com> Cc: beginners@haskell.org Message-ID: <aanlktinvkqfjusmtcwk6b4f_4vs4vv5vcfccxhqve...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" It's the type signature of hGet that is your undoing. it returns IO ByteString, and then you are trying to apply a pure function to that. You need to lift the pure function into the IO monad. On 1 July 2010 12:55, Tom Hobbs <tvho...@googlemail.com> wrote: > Hi guys, > > Thanks for all the previous help I'm having with serialization in Haskell, > I couldn't have gotten as far as I have without the help from this group. > Many thanks! > > Now I've put you in a good mood, maybe you can help me with my latest > problem... :-) > > I have the following code which reads strings from a handle; > > import qualified Data.ByteString.Lazy.UTF8 as UTF > > readNames 0 _ = [] > readNames n h = do > length <- fmap (fromIntegral . runGet > getWord32be) $ L.hGet h 4 > name <- L.hGet h length > (UTF.toString name) : readNames (n-1) h > > Where n is the number of names remaining to read and h is the handle to the > stream that I'm reading from. It is my intention that this function would > return a [String] of all the names. I'm sure further explainations probably > aren't necessary, but just in case; > > - "length" becomes equal to some number (represented by four bytes) which > describes the length of the string to read > - "name" becomes the string I'm trying to read > - Then I add that String to the list and go again > > But the problem I'm getting is this; > > Couldn't match expected type `[a]' against inferred type `IO b' > In a stmt of a 'do' expression: > length <- fmap (fromIntegral . runGet > getWord32be) $ L.hGet h 4 > In the expression: > do { length <- fmap (fromIntegral . runGet > getWord32be) > $ L.hGet h 4; > name <- L.hGet h length; > (UTF.toString name) : readNames (n - 1) h } > > In the definition of `readNames': > readNames n h > = do { length <- fmap (fromIntegral . runGet > getWord32be) > $ L.hGet h 4; > name <- L.hGet h length; > (UTF.toString name) : readNames (n - 1) h } > > I (think I) understand what the message is saying, but not why it's > occurring. I read the documentation as "UTF8.tostring" essentially rips the > [Char] out of the ByteString and returns that, so where does the "IO b" come > into it? Is it complaining because the reads from the handle are IO > operations and it's expecting me to use the IO monad somewhere? What i want > to be able to do is do the IO stuff inside the monad, but end up with a > [String] which I can then use in a pure function. > > RWH and Learn You A Good Haskell all cover some stream reading, but their > examples are to different from what I'm trying to do that I'm getting stuck. > > Any pointers are very gratefully accepted. > > Many thanks, > > Tom > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20100701/7122d4c0/attachment.html ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 25, Issue 1 ****************************************