[Haskell-beginners] Modeling recursive data structs

aditya siram aditya.siram at gmail.com
Sat Nov 6 00:14:12 EDT 2010


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
>


More information about the Beginners mailing list