Deferred Evaluation in Recursive Expressions?

birchb at ozemail.com.au birchb at ozemail.com.au
Tue May 9 08:12:22 EDT 2006


While working on type expressions I am rather stuck for a
way to express recursive types.  A simple example of this is a
singly-linked list of integers. In some languages you have compiler
syntax
which suspends evaluation so you can have recursive types. e.g.

   typedef Linked_List := int, Linked_List

In LISP I would use a macro.

I have tried using classes:

   class Linked_List(object):
         typedef = (int, Linked_List)

The closest I have got in Python is the following:

   Linked_List = (int, lambda: Linked_List)      # linked list of int

this is OK, because lambda makes closure which is not executed. However
it required the user of the type expression to call any lfunctions
found whilst traversing the tree.

To make this a little more OO, I could use a constructor to wrap the
function:

   Linked_List = (int, recursive(lambda: Linked_List))      # linked
list of int

but I am not satisfied with the "look".

Any suggestions?




More information about the Python-list mailing list