2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,14 +1,32 @@
|
|||
function range(begin, end) {
|
||||
for (let i = begin; i < end; ++i)
|
||||
yield i;
|
||||
}
|
||||
// USING A LIST MONAD DIRECTLY, WITHOUT SPECIAL SYNTAX FOR LIST COMPREHENSIONS
|
||||
|
||||
function triples(n) {
|
||||
return [[x,y,z] for each (x in range(1,n+1))
|
||||
for each (y in range(x,n+1))
|
||||
for each (z in range(y,n+1))
|
||||
if (x*x + y*y == z*z) ]
|
||||
}
|
||||
(function (n) {
|
||||
|
||||
for each (var triple in triples(20))
|
||||
print(triple);
|
||||
return mb(r(1, n), function (x) { // x <- [1..n]
|
||||
return mb(r(1 + x, n), function (y) { // y <- [1+x..n]
|
||||
return mb(r(1 + y, n), function (z) { // z <- [1+y..n]
|
||||
|
||||
return x * x + y * y === z * z ? [[x, y, z]] : [];
|
||||
|
||||
})})});
|
||||
|
||||
|
||||
// LIBRARY FUNCTIONS
|
||||
|
||||
// Monadic bind for lists
|
||||
function mb(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// Monadic return for lists is simply lambda x -> [x]
|
||||
// as in [[x, y, z]] : [] above
|
||||
|
||||
// Integer range [m..n]
|
||||
function r(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1))
|
||||
.map(function (n, x) {
|
||||
return m + x;
|
||||
});
|
||||
}
|
||||
|
||||
})(100);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue