Mark Engelberg wrote:

Chris,

I've been reading the CTM book, and am in Chapter 9.  I haven't
actually tried programming anything in Mozart yet, but your email
inspired me to pull up the Mozart environment and give the PowerSet
function a try.  Thanks for the inspiration!

Anyway, your program doesn't make a whole lot of sense to me.  First,
it looks like your PowerSet function would just show subsets formed by
dropping initial elements of the list (like [0,1,2],[1,2],[2]).  I'm
not experienced enough to see why your program would go into a wait
state.  Second, SolveAll as presented in the book expects a function
that takes no arguments.  So you have to wrap your function call.
It goes into a wait state because the output of PowerSet (an unbound variable, because PowerSet does not calculate a return value!) is passed to SolveAll (which expects a
one-argument function).  So SolveAll waits until this variable is bound.

Here's how I coded the PowerSet function using choice:  For each
element of the list, I choose whether to include the element or not.

declare
fun {Maybe N}
  choice
     [N]
  [] nil
  end
end

fun {PowerSet L}
  {Flatten {Map L Maybe}}
end

in
{Browse {SolveAll fun {$}{PowerSet [1 2 3]} end}}
A very nice solution, worthy of a good Prolog programmer. I like it because it's simple (it translates directly a declarative definition of the powerset concept) and
because it combines higher-order programming (the use of Map and Maybe) with
relational programming.

Peter


_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to