On 18.03.2006, at 08:25, Mark Engelberg wrote:
Yes, that's an iterative idiom, and it doesn't take too long to
understand. Nevertheless, you still need to figure out what the loop
is doing and what it's collecting. It's not nearly as elegant or
concise as:
[x#y | x <- [1..10], y<- [1..10], x mod y = 10]


Here is one way to do it more declaratively than by a loop ;-)

{SearchAll proc {$ X#Y#Z}
X :: 1#10
Y :: 1#10
Z = 0
%% NB: no implementation for FD.mod yet, only {FD.modI $D +I $D}
{FD.'mod' X Y Z}
end}


Here is another rather declarative way, which internally uses Filter instead of constraints

{MyListComprehension [{List.number 1 10 1}
{List.number 1 10 1}
[0]]
fun {$ [X Y Z]} X mod Y == Z end}


where

/** %% Collects in Zs the result of applying the binary function Fn on all possible combinations of Xs and Ys. The order in Zs is [{Fn X1 Y1} {Fn X1 Y2} ... {Fn X1 Yn} {Fn X2 Y1} ... {Fn Xn Yn}].
%% */
proc {MapCartesianProduct Xs Ys Fn Zs}
LYs = {Length Ys}
ZsAux = {MakeTuple unit {Length Xs}* LYs}
in
{List.forAllInd Xs
proc {$ I X}
{List.forAllInd Ys
proc {$ J Y}
ZsAux.(((I-1)*LYs)+J) = {Fn X Y}
end}
end}
Zs = {Record.toList ZsAux}
end
/** %% Xss is a list of lists and Test is a boolean function, expecting as many args as there are lists in Xss. Xss can be understood as a list of variables, where each variable is specified by a list of (arbitrary) domain values. MyListComprehension returns a list of all 'variable instantiations' for which Test returns true.
%% */
fun {MyListComprehension Xss Test}
{Filter {FoldR Xss fun {$ Xs Ys}
{MapCartesianProduct Xs Ys fun {$ X Y} X|Y end}
end
[nil]}
Test}
end

The function MyListComprehension could be used to define a macro for list comprehension. The macro only needs to add some syntactic sugar..


Best,
Torsten


On 3/17/06, Luis Quesada <[EMAIL PROTECTED]> wrote:
Maximilian Wilson wrote:
On 3/17/06, Mark Engelberg <[EMAIL PROTECTED]> wrote:

Is there any sort of linguistic support for list comprehensions? I
don't see anything in the manual, but it seems like the kind of thing
I'd expect Mozart to have.


A quick Google search tells me that list comprehensions are a
combination of List.map and List.filter. I take it you want syntactic
sugar to cut out the fun{$} definitions?

How about using collect
(http://www.mozart-oz.org/documentation/loop/node2.html#label5)?

This is how I would browse the list of tuples I#J such that 1=<I,J=<10
and I mod J =0:

{Browse
for I in 1..10 collect:C do
for J in 1..10 do
if I mod J==0 then {C I#J} end
end
end}

Luis

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


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


--
Torsten Anders
Sonic Arts Research Centre
Queen's University Belfast (UK)
www.torsten-anders.de
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to