I've started on translating examples from the book Purely Functional
Data Structures by Chris Okasaki into Oz. Only through chapter 2 so
far. The code will be posted at:
http://www.codepoetics.com/wiki/index.php?title=Topics:PFDS_in_other_languages:Oz
In translating the ML code, I ran across a question about whether Oz
functors can take parameters. In chapter 2.2, the example is an
UnbalancedSet that uses an Ordered functor for comparison operators. I
can emulate the parameters by instantiating the functor and then
immediately turning around and calling a function to initialize it with
the functor parameter. But I was wondering if I could send the
parameter as part of the Module.apply? This is what I am currently using:
[OrderedValue] = {Module.apply [ORDEREDVALUE]}
[UnbalancedSet] = {Module.apply [UNBALANCEDSET]}
{UnbalancedSet.initialize OrderedValue}
It would be nice if I could combine the effects of these last two
statements. (I may have asked this question before, but I don't recall
the resolution).
Here's the complete code from the translation:
% 2.2 UnbalancedSet
UNBALANCEDSET =
functor
export
initialize : Initialize
empty : Empty
member : Member
insert : Insert
define
Ordered
proc {Initialize OrderedSet}
Ordered = OrderedSet
end
Empty = nil
fun {Member X S}
case S
of tree(A Y B) then
if {Ordered.lt X Y} then
{Member X A}
elseif {Ordered.lt Y X} then
{Member X B}
else
true
end
else false
end
end
fun {Insert X S}
case S
of nil then tree(nil X nil)
[] tree(A Y B) then
if {Ordered.lt X Y} then
tree({Insert X A} Y B)
elseif {Ordered.lt Y X} then
tree(A Y {Insert X B})
else
S
end
end
end
end
ORDEREDVALUE =
functor
export
eq : EQ
lt : LT
leq : LEQ
define
fun {EQ X Y} X == Y end
fun {LT X Y} X < Y end
fun {LEQ X Y} X =< Y end
end
[OrderedValue] = {Module.apply [ORDEREDVALUE]}
[UnbalancedSet] = {Module.apply [UNBALANCEDSET]}
{UnbalancedSet.initialize OrderedValue}
MyStackA = {UnbalancedSet.insert 1 UnbalancedSet.empty}
MyStackB = {UnbalancedSet.insert 2 MyStackA}
MyStackC = {UnbalancedSet.insert 4 MyStackB}
MyStackD = {UnbalancedSet.insert 3 MyStackC}
{Browse MyStackD}
Thanks,
Chris Rathman
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users