Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:45:09+00:00 2026-05-15T23:45:09+00:00

I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform

  • 0

I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform and Shape nodes. In a SceneGraph the spatial transformation is accumulated while traversing and applied to the shape for rendering.

type Transform = Vector2 Double
data Shape     = Circle Double | Square Double
data SceneNode = XFormNode Transform | ShapeNode Shape

Say we have a scene with an object which is moved to the right and consisting of a Square at bottom and a Circle on top

^
|
|  ()
|  []
0----->

I came up with this tree definition:

let tree = Node (XFormNode (vector2 10 0))
                [Node (ShapeNode (Square 10)) []
                ,Node (XFormNode (vector2 0 10))
                      [Node (ShapeNode (Circle 10)) []]
                ]

The rendering would be something like this:

render :: Position2 -> Shape -> IO ()
render p (Circle r) = drawCircle p r
render p (Square a) = drawSquare p a

My questions are:

1) How do you define a traverse function, which accumulates the transformation and calls the render tasks?

2) How do you avoid making the traverse IO?

3) Is there a shorter version to define this tree? All but the first Node definition and all empty subForests are actually superfluous.

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:45:10+00:00Added an answer on May 15, 2026 at 11:45 pm

    Paradoxically, Data.Tree is not used very often in Haskell because defining a custom tree type is so easy. In your case, I would implement the scene graph (tree) as follows:

    type Transform = Vector2 Double
    data Shape     = Circle Double | Square Double
    data Scene     = Transform Transform [Scene] | Shape Shape
    

    Your example becomes

    example :: Scene
    example = Transform (vector2 10 0)
                        [ Shape (Square 10)
                        , Transform (vector2 0 10) [Shape (Circle 10)]
                        ]
    

    This answers point 3.

    To traverse the tree, use recursion:

    render :: Position2 -> Scene -> IO ()
    render p (Transform v scenes) = mapM_ (render (p+v)) scenes
    render p (Shape (Circle r))   = drawCircle p r
    render p (Shape (Square a))   = drawSquare p a
    

    There are more generic traversals available, for instance in Data.Traversable, but they are more “uniform”. In short, using recursion on trees is perfectly fine.

    Concering point 2, there is nothing much you can do once you decide that circles and squares should be rendered in the IO monad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MSDN example I want to implement some listener for simple class which periodically
I want to implement a simple wiki. Most probably will be using Java on
I want to implement simple socket client using ChannelPipelineFactory inside JBoss 7.0.2 AS. So
I want to implement a simple volume up/down button using a custom bitmap, and
I want to implement simple LINQ-to-SQL-like functionality in my .net application. For example i
I want to implement a simple script to do some boring housekeeping on my
I want to implement a simple help form to appear once the application has
I want to implement a simple link that when clicked, deletes the item from
I want to implement a simple specialized window manager for presentations (not user-controllable) that
I want to implement something simple like /System/Workflows/Sample Workflow with the small addition of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.