Data.Tree.Tree Show instance

Tomasz Zielonka t.zielonka@students.mimuw.edu.pl
Mon, 11 Aug 2003 19:16:08 +0200


Hi!

The Tree datatype in new Data.Tree module has a Show instance that works
like shown below:

  Prelude Data.Tree> print (Node 'a' [Node 'b' [], Node 'c' []])
  - 'a' -+- 'b'
	 |
	 `- 'c'

I use a similar module for Trees in conjunction with WASH for generating
web pages. WASH session management relies on the ability to read back
previously showed values. 

Writing a Read instance for such Show would be a weird thing to do.

I think it would be better to derive Show instance for Tree instead of
providing a pretty printing one. Then it would be possible to have a
complementing Read instance. The pretty printing function could be
provided under a different name (eg. drawTree).

The other tiny problem is that the current instance is not
recursion-proof:

  Prelude Data.Tree> let f x = Node x [Node x [], Node x []]
  Prelude Data.Tree> print (f (f 1))
  - - 1 -+- 1
       |
       `- 1
   -+- - 1 -+- 1
       |
       `- 1

				   |
				   `- - 1 -+- 1
       |
       `- 1

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links