/* Return multiple values (Rosetta code) in Picat. http://rosettacode.org/wiki/Return_multiple_values """ Show how to return more than one value from a function. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ /* Functions returns a single value. Multiple values must be collected in a list (or an array, map, set, structure). Predicates Sometimes it is not possible - or not convenient - to create a function that return values. In those cases a predicate is used and some of the variables are considered output values and are usually placed as the last parameters. A variant of this is to use a structure (here $ret(A,B,C)) that collects the output values. (In some cases the name input/output values are muzzled when a predicate can be used with multiple input/output modes, e.g. reversible predicates such as append/3 or member/2.) */ main => go. go => [A,B,C] = fun(10), pred(10, E,F,G), println([E,F,G]), pred2(10, Ret), println(Ret), % or pred2(10,$ret(H,I,J)), println([H,I,J]), nl. fun(N) = [2*N-1,2*N,2*N+1]. % A, B, and C are output values pred(N, A,B,C) => A=2*N-1, B=2*N, C=2*N+1. % The structure $ret(A,B,C) is the output value. pred2(N, ret(A,B,C)) :- A=2*N-1, B=2*N, C=2*N+1.