home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / src / various / trees.e < prev   
Text File  |  2001-03-31  |  830b  |  44 lines

  1. /* A different style of programming in E:
  2.    working with and building large dynamic datastructures
  3.    without using the keyword PTR
  4.  
  5.         / \
  6.        /   \
  7.      /       \
  8.    / \       / \
  9.  /\   /\   /\   /\ 
  10. 1  2 3  4 5  6 7  8
  11.  
  12. */
  13.  
  14. PROC main()
  15.   DEF tree,a
  16.   tree:=node(
  17.           node(
  18.             node(leaf(1),leaf(2)),
  19.             node(leaf(3),leaf(4))
  20.           ),
  21.           node(
  22.             node(leaf(5),leaf(6)),
  23.             node(leaf(7),leaf(8))
  24.           )
  25.         )
  26.   WriteF('sum = \d\n',sum(tree))
  27.   FOR a:=1 TO 10
  28.     tree:=node(leaf(100),tree)
  29.     WriteF('sum = \d\n',sum(tree))
  30.   ENDFOR
  31. ENDPROC
  32.  
  33. PROC node(l,r) IS NEW ["node",l,r]
  34. PROC leaf(n) IS NEW ["leaf",n]
  35.  
  36. PROC sum(t)
  37.   DEF left,right,n
  38.   IF t <=> ["node",left,right]
  39.     RETURN sum(left)+sum(right)
  40.   ELSEIF t <=> ["leaf",n]
  41.     RETURN n
  42.   ENDIF
  43. ENDPROC
  44.