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. Re: Fwd: My first functioning haskell project - a steganography utility (Brandon S Allbery KF8NH) 2. why is something different within a function when it comes out? (prad) 3. regular expressions (prad) 4. Re: why is something different within a function when it comes out? (Michael Mossey) 5. Re: why is something different within a function when it comes out? (Chadda? Fouch?) 6. newtype record syntax (Tom Doris) 7. Re: newtype record syntax (Brent Yorgey) 8. Re: newtype record syntax (Tom Doris) ---------------------------------------------------------------------- Message: 1 Date: Tue, 13 Jul 2010 14:11:28 -0400 From: Brandon S Allbery KF8NH <allb...@ece.cmu.edu> Subject: Re: [Haskell-beginners] Fwd: My first functioning haskell project - a steganography utility To: beginners@haskell.org Message-ID: <4c3cac50.7020...@ece.cmu.edu> Content-Type: text/plain; charset=UTF-8 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 7/13/10 11:20 , Tim Cowlishaw wrote: > actually, looking at it again, it'd be something like > > maybeMonoid :: (Monoid a) => Maybe a -> Maybe a -> a > maybeMonoid :: x y = (fromMaybe mempty x) `mappend` (fromMaybe mempty y) Which in turn can be written as: > maybeMonoid = mappend `on` (fromMaybe mempty) ("on" is in Data.Function: "g `on` f" expresses the "\x y -> (f x) `g` (f y)" idiom.) - -- brandon s. allbery [linux,solaris,freebsd,perl] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.10 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkw8rE8ACgkQIn7hlCsL25XrugCdFLhDalZflo2P8h4XkdCODqAw tcEAn0siZ8CK0+NKay0dz991kBxkvEjh =Actk -----END PGP SIGNATURE----- ------------------------------ Message: 2 Date: Tue, 13 Jul 2010 22:33:10 -0700 From: prad <p...@towardsfreedom.com> Subject: [Haskell-beginners] why is something different within a function when it comes out? To: haskellbeginners <beginners@haskell.org> Message-ID: <20100713223310.1cf90...@gom> Content-Type: text/plain; charset=US-ASCII i have this: main = do c <- readFile "test.txt" let tleP = "<title>\n(.*)\n</title>" let tle = c=~tleP::[[String]] putStrLn $ tle!!0!!1 let g = xtract tleP c putStrLn $ show g xtract p c = do let r = c=~p::[[String]] return r!!0!!0!!1 for the first putStrLn i get: League of Humane Voters Home Page for the second i get "League of Humane Voters Home Page" but only after i have done r !!0 !!0 !!1 whereas i only needed tle !!0 !!1 it seems to me that both tle and r serve the same purpose though they come out as different types r being a string and tle being i don't know what because i get a "Not in scope: 'tle' so even though both r and tle are set as [[String]], they don't seem to be the same creature. -- In friendship, prad ... with you on your journey Towards Freedom http://www.towardsfreedom.com (website) Information, Inspiration, Imagination - truly a site for soaring I's ------------------------------ Message: 3 Date: Tue, 13 Jul 2010 23:37:45 -0700 From: prad <p...@towardsfreedom.com> Subject: [Haskell-beginners] regular expressions To: haskellbeginners <beginners@haskell.org> Message-ID: <20100713233745.5463c...@gom> Content-Type: text/plain; charset=US-ASCII there seem to be a lot of regular expression modules: Text.Regex.TDFA Text.Regex.PCRE Text.Regex.PCRE.Light there is a bit info on how to use it in real world haskell but only for the POSIX stuff (eg TDFA) any suggestions or preferences? -- In friendship, prad ... with you on your journey Towards Freedom http://www.towardsfreedom.com (website) Information, Inspiration, Imagination - truly a site for soaring I's ------------------------------ Message: 4 Date: Wed, 14 Jul 2010 01:10:04 -0700 From: Michael Mossey <m...@alumni.caltech.edu> Subject: Re: [Haskell-beginners] why is something different within a function when it comes out? To: prad <p...@towardsfreedom.com> Cc: haskellbeginners <beginners@haskell.org> Message-ID: <4c3d70dc.80...@alumni.caltech.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed I'm fairly new, so I can't fully explain the behavior you got, but I know at least one thing you did wrong: prad wrote: > i have this: > > > main = do > > c <- readFile "test.txt" > let tleP = "<title>\n(.*)\n</title>" > let tle = c=~tleP::[[String]] > putStrLn $ tle!!0!!1 > > let g = xtract tleP c > putStrLn $ show g > > > xtract p c = do > let r = c=~p::[[String]] > return r!!0!!0!!1 > You probably mean to write > xtract p c = r !! 0 !! 1 > where r = c=~p::[[String]] You used do-notation when you meant to write a simple function. The use of "return" in do-notation is one point of confusion with beginners. It does not operate like "return" in an imperative language. I'm not sure what your 'xtract' did, but the compiler probably accepted the do-notation because it specified a list monad of some sort. I recommend you pay close attention to introductory examples, noticing in particular when they are unlike imperative languages. Find some tutorials on monads and the do-notation. Best, Mike ------------------------------ Message: 5 Date: Wed, 14 Jul 2010 10:54:22 +0200 From: Chadda? Fouch? <chaddai.fou...@gmail.com> Subject: Re: [Haskell-beginners] why is something different within a function when it comes out? To: prad <p...@towardsfreedom.com> Cc: haskellbeginners <beginners@haskell.org> Message-ID: <aanlktik3vxlgxpymdaysyggkh06eejkyo8en5wqnw...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Wed, Jul 14, 2010 at 7:33 AM, prad <p...@towardsfreedom.com> wrote: > xtract p c = do >   let r = c=~p::[[String]] >   return r!!0!!0!!1 return is just a function in Haskell, and given that function application has priority over any operator, this mean your last line is : > (return r) !! 0 !! 0 !! 1 return type is "(Monad m) => a -> m a" where m is a type constructor variable (a type constructor is a parametrized type, think array parametrized on the element type in most language, or template in C++) constrained to be an instance of the Monad typeclass. Monad is an important typeclass in Haskell but here it's enough to look at (!!) type "[a] -> Int -> a" to see that (return r) should be of a list type, list is a monad so this code typecheck, but return is perfectly redundant : For the list monad : > return x = [x] so in this case your last line put r in a singleton list : > [r] !! 0 !! 0 !! 1 then (!! 0) extract r > r !! 0 !! 1 And you get what you wanted in the first place... So you could write just "r !! 0 !! 1" instead of "return r!!0!!!0!!1" for the same result. In fact xtract could be written : > xtract p c = (c =~ p :: [[String]]) !! 0 !! 1 do-notation (which is just syntactic sugar to write monadic code easily) and return (which is just a function, not a keyword) are only useful when you're working with monads, if you're not you shouldn't use them. -- Jedaï ------------------------------ Message: 6 Date: Wed, 14 Jul 2010 10:39:23 +0100 From: Tom Doris <tomdo...@gmail.com> Subject: [Haskell-beginners] newtype record syntax To: beginners@haskell.org Message-ID: <aanlktily0bzplho0nylxgjwifnh5cb1-hwoxq_pey...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Can someone give an explanation of the use of record syntax in newtype declarations that helps one understand how it's meant to be interpreted? I understand the "facts" according to the language definition, but I just don't see why this notation was used; does it have any relationship with records? The only docs I can find that cover this are in the language report itself: >|A newtype declaration may use field-naming syntax, though of course there may only be one field. Thus: >| newtype Age = Age { unAge :: Int } >|brings into scope both a constructor and a de-constructor: >| Age :: Int -> Age >| unAge :: Age -> Int I understand what this says, I just don't understand why Haskell does it this way - unAge ends up defining a function, using the notation used to declare a field in a record, and the type of the function is inferred from the type of the field and the constructor - it just seems a bit barmy. I'd appreciate anyone who can help, or a reference to some docs that make sense of this. Thanks t -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20100714/54093699/attachment-0001.html ------------------------------ Message: 7 Date: Wed, 14 Jul 2010 11:00:12 +0100 From: Brent Yorgey <byor...@seas.upenn.edu> Subject: Re: [Haskell-beginners] newtype record syntax To: beginners@haskell.org Message-ID: <20100714100012.ga28...@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii On Wed, Jul 14, 2010 at 10:39:23AM +0100, Tom Doris wrote: > Can someone give an explanation of the use of record syntax in newtype > declarations that helps one understand how it's meant to be interpreted? I > understand the "facts" according to the language definition, but I just > don't see why this notation was used; does it have any relationship with > records? Yes, the reason this notation is used is that it corresponds exactly to defining a record containing one field. Declaring field names in a record really just gives you some projection functions to get the fields out (and also some pattern-matching magic); declaring a field name in a newtype gives you a projection function to unwrap the newtype. Does that help? -Brent ------------------------------ Message: 8 Date: Wed, 14 Jul 2010 12:41:52 +0100 From: Tom Doris <tomdo...@gmail.com> Subject: Re: [Haskell-beginners] newtype record syntax To: Brent Yorgey <byor...@seas.upenn.edu> Cc: beginners@haskell.org Message-ID: <aanlktil5b-nkogqjcysbwyvyzxrvkc0aflcxav3mz...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Yes, makes sense, I always thought of the projection functions as being secondary. Thanks On 14 July 2010 11:00, Brent Yorgey <byor...@seas.upenn.edu> wrote: > On Wed, Jul 14, 2010 at 10:39:23AM +0100, Tom Doris wrote: > > Can someone give an explanation of the use of record syntax in newtype > > declarations that helps one understand how it's meant to be interpreted? > I > > understand the "facts" according to the language definition, but I just > > don't see why this notation was used; does it have any relationship with > > records? > > Yes, the reason this notation is used is that it corresponds exactly > to defining a record containing one field. Declaring field names in a > record really just gives you some projection functions to get the > fields out (and also some pattern-matching magic); declaring a field > name in a newtype gives you a projection function to unwrap the > newtype. > > Does that help? > > -Brent > _______________________________________________ > 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/20100714/e88edef4/attachment.html ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 25, Issue 31 *****************************************