|
From: john s. <sk...@us...> - 2015-03-31 13:34:55
|
here is some sample code using sets
// find all the numbers < 10 whose squares are the sum of two primes
begin
var primes = (2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79,
83, 89, 97 )
;
var squares = (1,4,9,16,25,36,49,64,81,100);
var primepairs = {x,y : int * int | x \in primes and y \in primes};
//var sumissquare = {i,j: int^2 | i + j \in squares };
fun sum (i:int, j:int) => i + j;
var sumissquare = invimg (sum, squares);
var square_sum_of_primes = sumissquare \cap primepairs;
for var i in 1 upto 100 do
for var j in i upto 100 do
if (i,j) \in square_sum_of_primes do
println$ (i+j).str + "="+i.str+"+"+j.str;
done
done
done
end
Now, the thing to note here is that the kind of sets we have here
are just fancy predicates. We can form the conjunction of two
predicates like this:
fun (x:int) => P x and Q x
so what's the big deal with sets?
Well, you don't need the "fun" binder to compose them
{ x:int | P x } \cap { y:int | Q y }
In fact I'm going to implement this:
P and Q
as well :)
--
john skaller
sk...@us...
http://felix-lang.org
|