https://svn.origo.ethz.ch/algebraist/trunk/aldor/lib/axllib/test/tree.as
Well, you'd have to remove semicola and braces, of course and add the
)abbrev line, but it looks pretty much like what you want.
tree.as : I fear dealing with Aldor + Spad will be even more difficult.
for me.
I did not suggest to *use* aldor. But rather to take the code as an example.
Your code looked like having a bit of overhead. I cheated a bit with the
empty tree, by going back to LISP (see attachment). Well, if Record
exported a nil value for an empty record, that wouldn't have been
necessary. But Record doesn't (yet).
And all this -- for expressing of what is expressed in a single line
in Haskell.
Well, OK a lot of boilerplate in SPAD, but remember SPAD is not
functional and a rather different kind of language than Haskell.
Hope, by now you see the pattern.
Ralf
(1) -> B := BinaryTree(Integer)
(1) BinaryTree(Integer)
Type: Type
(2) -> empty()$B
(2) ()
Type: BinaryTree(Integer)
(4) -> t==>tree$B
Type: Void
(6) -> b := t(5, t 4, t 6)
(6) (5 (4 () ()) (6 () ()))
Type: BinaryTree(Integer)
(7) -> # b
(7) 3
Type: PositiveInteger
(8) -> sumTree b
(8) 15
Type: PositiveInteger
--
You received this message because you are subscribed to the Google Groups "FriCAS -
computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fricas-devel?hl=en.
rep x ==> (x@%) pretend Rep
per x ==> (x@Rep) pretend %
OF ==> OutputForm
)abbrev domain BTREE BinaryTree
BinaryTree(S: SetCategory): SetCategory with
empty: () -> %
tree: S -> %
tree: (S, %, %) -> %
left: % -> %
right: % -> %
node: % -> S
coerce: % -> OutputForm
_#: % -> Integer
if S has AbelianMonoid then
sumTree: % -> S
== add
Rep == Record(node: S, left: %, right: %)
empty(): % == per(NIL$Lisp)
empty?(x: %): Boolean == NULL(x)$Lisp
coerce(x: %): OutputForm ==
empty? x => "()"::OF
hconcat["(", node(x)::OF, " "::OF, left(x)::OF,
" "::OF, right(x)::OF, ")"]
tree(s: S): % == per [s, empty(), empty()]
tree(s: S, l: %, r: %): % == per [s, l, r]
node(x: %): S == rep(x).node
left(x: %): % == rep(x).left
right(x: %): % == rep(x).right
((x: %) = (y: %)): Boolean ==
empty? x =>
empty? y => true
false
empty? y => false
node x = node y and left x = left y and right x = right y
#(x: %): Integer ==
empty? x => 0
1 + # left x + # right x
if S has AbelianMonoid then
sumTree(x: %): S ==
empty? x => 0
node(x) + sumTree left x + sumTree right x