[Haskell-beginners] Modeling recursive data structs
Markus Läll
markus.l2ll at gmail.com
Sat Nov 6 06:01:30 EDT 2010
Try this: data Super a = A a | B [a] | C [Super a]
--
Markus Läll
On Sat, Nov 6, 2010 at 6:14 AM, aditya siram <aditya.siram at gmail.com> wrote:
> Does the following code help?
>
> type Point = (Int,Int)
> type PolyLines = [Point]
> data Shape = Point Point | PolyLines PolyLines | Shape [Shape]
> -- here you might want to write:
> -- data Shape = Point | PolyLines | [Shape]
> -- but Haskell requires that "type constructors" like "data Shape ..."
> -- use "data constructors" like "Point ...", "PolyLines ..." to group
> -- different values.
> -- Type synonyms, however, don't require a data constructor but
> -- are restricted to one value
>
> -- Example usage :
> shapes = Shape [(Point (1,2)), PolyLines [(2,3),(4,5)],
> Shape[Shape[Point (6,7)]]]
>
> -deech
>
> On Fri, Nov 5, 2010 at 10:37 PM, Nikolay Artamonov <nartamonov at gmail.com> wrote:
>> Hi all! How can we model in Haskell three hypothetic data structures
>> A, B, C which obey following conditions:
>>
>> 1. A is anything
>> 2. B is list of A's
>> 3. C is list of A's or B's or another C's
>>
>> So, for example, it would be possible to write something like: C [A, B
>> [A, A], A, C [A, B [A]]].
>>
>> I wrote this code:
>>
>> data Super = A | B [A] | C[Super]
>>
>> Not surprisingly, Haskell refused to accept subexpression `B[A]`,
>> because "Not in scope: type constructor or class `A'". How to solve
>> this problem?
>>
>> More "real" example of A, B and C:
>>
>> data Shape = Point Int Int | Polyline [Point] | Shape [Shape]
>>
>> Interestingly, if I replace Polyline [Point] to Polyline [Shape] then
>> definition will be accepted. But what if I want *polylines* consists
>> of *points*, not any *shapes*?
>>
>> Thank you!
>>
>> --
>> Nikolay Artamonov
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list