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. represent data sturcture using function (Raeck chiu) 2. Re: about Data.BinaryTree (Daniel Fischer) 3. Re: Re: Restricting integers in a type? (Colin Paul Adams) 4. 'Iterating a data type' (ra...@msn.com) 5. Re: Restricting integers in a type? (Ertugrul Soeylemez) 6. Re: Re: Restricting integers in a type? (Colin Paul Adams) 7. Re: 'Iterating a data type' (Paul Visschers) 8. Enum and Bounded in generic type (ra...@msn.com) 9. Re: Enum and Bounded in generic type (Philippa Cowderoy) 10. Re: Enum and Bounded in generic type (Paul Visschers) ---------------------------------------------------------------------- Message: 1 Date: Mon, 29 Dec 2008 05:04:28 +0000 From: Raeck chiu <ra...@msn.com> Subject: [Haskell-beginners] represent data sturcture using function To: Beginners Haskell <beginners@haskell.org>, Cafe Haskell <haskell-c...@haskell.org> Message-ID: <bay113-w46079ce299019f937372e2bb...@phx.gbl> Content-Type: text/plain; charset="windows-1252" Merry Christmas! Hope everybody is enjoying the Christmas! I am doing some readings and I found something seems to be interesting. People sometime will try to represent a quantity-regard-only data structure (such a order-regadless List) using functions instead of ta concrete data structure (like the haskell build-in []), any particular reason for this? For example, > data Sex = Male | Female > classA :: Sex -> Int > classA Male = 100 > classA Female = 200 when I should prefer the above solution instead of using a concrete data structure such as [] to represent the classA members? It seems to be very difficult to change the number of Male or Female if a concrete data structure is not used. Is it possible change the number of Male in classA when represent classA using function? Thank you very much! Best wishes, Raeck _________________________________________________________________ Its the same Hotmail®. If by same you mean up to 70% faster. http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_broad1_122008 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081229/78f7e2f7/attachment-0001.htm ------------------------------ Message: 2 Date: Mon, 29 Dec 2008 07:06:26 +0100 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] about Data.BinaryTree To: "Liu Jian" <gjk....@gmail.com>, beginners@haskell.org Message-ID: <200812290706.26856.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-1" Am Montag, 29. Dezember 2008 04:22 schrieb Liu Jian: > Hi All, > > Is there any implementation of binary tree in haskell library? for > example, "insert lookup empty adjust isEmpty delete" including in > it. > > cheers, Well, depending on your use-case, it might be worth looking at Data.Set and Data.Map, they're implemented via binary trees. There's an AVL-tree imlementation in http://hackage.haskell.org/cgi-bin/hackage-scripts/package/collections And generally a lot of data structures on Hackage, suffix-trees, finger-trees... There should be something that suits your needs. Cheers, Daniel ------------------------------ Message: 3 Date: Mon, 29 Dec 2008 06:37:12 +0000 From: Colin Paul Adams <co...@colina.demon.co.uk> Subject: Re: [Haskell-beginners] Re: Restricting integers in a type? To: Ertugrul Soeylemez <e...@ertes.de> Cc: beginners@haskell.org Message-ID: <m37i5jcwrr....@colina.demon.co.uk> Content-Type: text/plain; charset=us-ascii >>>>> "Ertugrul" == Ertugrul Soeylemez <e...@ertes.de> writes: >> That would seem to allow x = -3 and y = 13, for instance. Ertugrul> Not if you disallow an x = -3 and y = 13 to happen in Ertugrul> the first place. Haskell's module and type system Ertugrul> allows you to do that. Can you explain how to do that please? -- Colin Adams Preston Lancashire ------------------------------ Message: 4 Date: Tue, 30 Dec 2008 01:24:00 -0000 From: <ra...@msn.com> Subject: [Haskell-beginners] 'Iterating a data type' To: "Beginners Haskell" <beginners@haskell.org>, "Cafe Haskell" <haskell-c...@haskell.org> Message-ID: <bay113-ds6f73b9aff90789738fe8fbb...@phx.gbl> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Are there anyway to express the "iterating" of a user-defined data type in Haskell? For example, in > data Shape = Square | Circle | Triangle how can I 'iterate' them and apply them all to the same function without indicating them explicitly? such as [func Square, func Circle, func Triangle]. Can I use a form similar to the following case in a list instead: > Numbers = [1,2,3,4,5] > [func x | x <- Numbers ] Actually what I want is to obtain all the possible values of a data type (Shape). Thank you very much! Best wishes, Raeck ------------------------------ Message: 5 Date: Tue, 30 Dec 2008 10:13:15 +0100 From: Ertugrul Soeylemez <e...@ertes.de> Subject: [Haskell-beginners] Re: Restricting integers in a type? To: beginners@haskell.org Message-ID: <20081230101315.4692a...@tritium.xx> Content-Type: text/plain; charset=US-ASCII Colin Paul Adams <co...@colina.demon.co.uk> wrote: > >>>>> "Ertugrul" == Ertugrul Soeylemez <e...@ertes.de> writes: > > >> That would seem to allow x = -3 and y = 13, for instance. > > Ertugrul> Not if you disallow an x = -3 and y = 13 to happen in > Ertugrul> the first place. Haskell's module and type system > Ertugrul> allows you to do that. > > Can you explain how to do that please? Sure: module Even (Even) where newtype Even a = Even a deriving (Eq, Show) instance Integral a => Num (Even a) where Even a + Even b = Even (a+b) -- ... fromInteger x = if even x then Even (fromInteger x) else undefined The only way to introduce incorrect Even values would be to access the constructor directly, but in that example, it's not exported. The interface to constructing Even values is the fromInteger function. Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/ ------------------------------ Message: 6 Date: Tue, 30 Dec 2008 09:15:42 +0000 From: Colin Paul Adams <co...@colina.demon.co.uk> Subject: Re: [Haskell-beginners] Re: Restricting integers in a type? To: Ertugrul Soeylemez <e...@ertes.de> Cc: beginners@haskell.org Message-ID: <m37i5iaurl....@colina.demon.co.uk> Content-Type: text/plain; charset=us-ascii >>>>> "Ertugrul" == Ertugrul Soeylemez <e...@ertes.de> writes: Ertugrul> Colin Paul Adams <co...@colina.demon.co.uk> wrote: >> >>>>> "Ertugrul" == Ertugrul Soeylemez <e...@ertes.de> writes: >> >> >> That would seem to allow x = -3 and y = 13, for instance. >> Ertugrul> Not if you disallow an x = -3 and y = 13 to happen in Ertugrul> the first place. Haskell's module and type system Ertugrul> allows you to do that. >> >> Can you explain how to do that please? Ertugrul> Sure: Ertugrul> module Even (Even) where Ertugrul> newtype Even a = Even a deriving (Eq, Show) Ertugrul> instance Integral a => Num (Even a) where Even a + Ertugrul> Even b = Even (a+b) -- ... fromInteger x = if even x Ertugrul> then Even (fromInteger x) else undefined Ertugrul> The only way to introduce incorrect Even values would be Ertugrul> to access the constructor directly, but in that example, Ertugrul> it's not exported. The interface to constructing Even Ertugrul> values is the fromInteger function. Thanks for all your help. -- Colin Adams Preston Lancashire ------------------------------ Message: 7 Date: Tue, 30 Dec 2008 11:30:17 +0100 From: Paul Visschers <m...@paulvisschers.net> Subject: Re: [Haskell-beginners] 'Iterating a data type' To: ra...@msn.com Cc: beginners@haskell.org Message-ID: <4959f839.6050...@paulvisschers.net> Content-Type: text/plain; charset=ISO-8859-1 You actually have two different questions. The first about iteration can be done by the function map in the following way: Instead of [func Square, func Circle, func Triangle] you use: map func [Square, Circle, Triangle]. The list comprehensions should also work: [func x | x <- [Square, Circle, Triangle]] Now as for obtaining/generating all values of Shape, the easiest way is to make Shape an instance of Enum, like this: data Shape = Square | Circle | Triangle deriving Enum You can then generate a list of all the values by: enumFrom Square You use Square here because it is the first constructor of Shape, and you want to enumerate them all. I hope this helps, Paul ra...@msn.com wrote: > Are there anyway to express the "iterating" of a user-defined data type > in Haskell? > > For example, in > >> data Shape = Square | Circle | Triangle > > how can I 'iterate' them and apply them all to the same function without > indicating them explicitly? > such as [func Square, func Circle, func Triangle]. > Can I use a form similar to the following case in a list instead: > >> Numbers = [1,2,3,4,5] > >> [func x | x <- Numbers ] > > Actually what I want is to obtain all the possible values of a data type > (Shape). > > Thank you very much! > > Best wishes, > > Raeck > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ Message: 8 Date: Tue, 30 Dec 2008 17:12:29 -0000 From: <ra...@msn.com> Subject: [Haskell-beginners] Enum and Bounded in generic type To: <haskell-c...@haskell.org>, <beginners@haskell.org> Message-ID: <bay113-ds25d81b87ba450b73be194bb...@phx.gbl> Content-Type: text/plain; format=flowed; charset="ISO-8859-1"; reply-type=original Hi, how can I make the following code work? (show all the possible values of a type 'a') > showAll :: (Eq a, Bounded a, Enum a) => a -> [a] > showAll a = [minBound..maxBound]::[a] it keeps saying that I could fix the problem by adding (Enum a) and (Bounded a) the the context, but I failed when I try. anything goes wrong? Thanks! Raeck ------------------------------ Message: 9 Date: Tue, 30 Dec 2008 17:17:37 +0000 From: Philippa Cowderoy <fli...@flippac.org> Subject: Re: [Haskell-beginners] Enum and Bounded in generic type To: ra...@msn.com Cc: beginners@haskell.org Message-ID: <1230657457.1125.1048.ca...@flippa-eee> Content-Type: text/plain [-cafe left out] On Tue, 2008-12-30 at 17:12 +0000, ra...@msn.com wrote: > Hi, how can I make the following code work? (show all the possible values of > a type 'a') > > > showAll :: (Eq a, Bounded a, Enum a) => a -> [a] > > showAll a = [minBound..maxBound]::[a] > > it keeps saying that I could fix the problem by adding (Enum a) and (Bounded > a) the the context, but I failed when I try. > The problem is the inner annotation, ::[a]. It's not the same a as in (Eq a, Bounded a, Enum a) => a -> [a], and in Haskell98 it can't be. I'm not surprised that's confused you! If you remove the inner annotation it should work fine though. You can go a step further and make showAll a value rather than a function: showAll = [minBound..maxBound] (I've left out the type annotations, but you can put ":t [minBound..maxBound]" into ghci or hugs if you want one) -- Philippa Cowderoy <fli...@flippac.org> I left the snappy .sigs on the other computer ------------------------------ Message: 10 Date: Tue, 30 Dec 2008 18:50:22 +0100 From: Paul Visschers <m...@paulvisschers.net> Subject: Re: [Haskell-beginners] Enum and Bounded in generic type To: ra...@msn.com Cc: beginners@haskell.org Message-ID: <495a5f5e.70...@paulvisschers.net> Content-Type: text/plain; charset=ISO-8859-1 You seem to be having some trouble with the type system, not just in this instance. I found when I was learning Haskell that when this happens, it is useful to not add type annotations, then load it up in GHCi and see what it comes up with (with the aforementions :t option). Then you can see why that makes sense and possibly change the function. Once you're happy with the function and are confident you understand its type, go ahead and put the type annotation back into your source code. I also found it useful to play around with various expressions and see what their types where. Especially with things like partial application, function composition and monadic functions. Paul Philippa Cowderoy wrote: > [-cafe left out] > > On Tue, 2008-12-30 at 17:12 +0000, ra...@msn.com wrote: >> Hi, how can I make the following code work? (show all the possible values of >> a type 'a') >> >>> showAll :: (Eq a, Bounded a, Enum a) => a -> [a] >>> showAll a = [minBound..maxBound]::[a] >> it keeps saying that I could fix the problem by adding (Enum a) and (Bounded >> a) the the context, but I failed when I try. >> > > The problem is the inner annotation, ::[a]. It's not the same a as in > (Eq a, Bounded a, Enum a) => a -> [a], and in Haskell98 it can't be. I'm > not surprised that's confused you! If you remove the inner annotation it > should work fine though. > > You can go a step further and make showAll a value rather than a > function: > > showAll = [minBound..maxBound] > > (I've left out the type annotations, but you can put ":t > [minBound..maxBound]" into ghci or hugs if you want one) > ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 6, Issue 10 ****************************************