[kepler-dev] Re: type signatures in the expression language

Bertram Ludaescher ludaesch at sdsc.edu
Wed Aug 4 06:29:00 PDT 2004


>>>>> "TF" == Tobin Fricke <tobin at splorg.org> writes:
...
TF> Specific application: say I have an array x={double} and y={double} and I
TF> want to produce {{x=double,y=double,z=double}} for all pairs of values
TF> (x,y) and with z = f(x,y) for a function f.  Can/should this be described
TF> in the expression language or should I just write the java actor?

Just fyi: here is this in Haskell:

  pair_f xs ys f = [(x, y, (f x y))| x <- xs, y <- ys]

(can't describe it much simpler than that ;-)

Together with
  mult x y = x * y
and 
  go = pair_f [1,2,3] [5,7] mult

this gives:
  Main> go
  [(1,5,5),(1,7,7),(2,5,10),(2,7,14),(3,5,15),(3,7,21)] :: [(Integer,Integer,Integer)]

A similar solution in Prolog looks like that (the higher-order stuff
for 'f' instead of 'mult' would require slightly more effort): 

--------------------------------------
multiply(X,Y,Z) :-
	Z is X*Y.

pair_f(Xs, Ys, X, Y, Z) :-
	member(X, Xs),
	member(Y, Ys),
	multiply(X,Y,Z). 

go :-
	setof( (X,Y,Z),
	       pair_f([1,2,3], [5,7], X, Y, Z),
	       Triples),
	write(Triples).
--------------------------------------

When run, this yields:

  ?- go.
  [ (1, 5, 5), (1, 7, 7), (2, 5, 10), (2, 7, 14), (3, 5, 15), (3, 7, 21)]
  Yes


Bertram



More information about the Kepler-dev mailing list