June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,5 +1,3 @@
{{Omit From|C}}
{{Omit From|Java}}
{{Omit From|Modula-3}}
{{omit from|ACL2}}
{{omit from|BBC BASIC}}

View file

@ -51,24 +51,19 @@ end run
-- (>>=) :: Monad m => m a -> (a -> m b) -> m b
on |>>=|(xs, f)
concat(map(f, xs))
concatMap(f, xs)
end |>>=|
-- concat :: [[a]] -> [a] | [String] -> String
on concat(xs)
script append
on |λ|(a, b)
a & b
end |λ|
end script
if length of xs > 0 and class of (item 1 of xs) is string then
set empty to ""
else
set empty to {}
end if
foldl(append, empty, xs)
end concat
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set acc to {}
tell mReturn(f)
repeat with x in xs
set acc to acc & |λ|(contents of x)
end repeat
end tell
return acc
end concatMap
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
@ -84,30 +79,6 @@ on enumFromTo(m, n)
return lst
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)

View file

@ -0,0 +1 @@
for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }

View file

@ -0,0 +1,2 @@
int i;
for (i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }

View file

@ -0,0 +1 @@
triplets n = [(x, y, z) | x <- [1 .. n], y <- [x .. n], z <- [y .. n]]

View file

@ -0,0 +1 @@
[(x, y, z) | (x, y, z) <- triplets n, x^2 + y^2 == z^2]

View file

@ -0,0 +1,33 @@
// Boilerplate
import java.util.Arrays;
import java.util.List;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public interface PythagComp{
static void main(String... args){
System.out.println(run(20));
}
static List<List<Integer>> run(int n){
return
// Here comes the list comprehension bit
// input stream - bit clunky
range(1, n).mapToObj(
x -> range(x, n).mapToObj(
y -> range(y, n).mapToObj(
z -> new Integer[]{x, y, z}
)
)
)
.flatMap(identity())
.flatMap(identity())
// predicate
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
// output expression
.map(Arrays::asList)
// the result is a list
.collect(toList())
;
}
}

View file

@ -1 +1,41 @@
[[3, 4, 5], [5, 12, 13], [6, 8, 10], [8, 15, 17], [9, 12, 15], [12, 16, 20]]
(n => {
'use strict';
// GENERIC FUNCTIONS ------------------------------------------------------
// bind (>>=) :: Monad m => m a -> (a -> m b) -> m b
const bind = (m, mf) =>
Array.isArray(m) ? (
bindList(m, mf)
) : bindMay(m, mf);
// bindList (>>=) :: [a] -> (a -> [b]) -> [b]
const bindList = (xs, mf) => [].concat.apply([], xs.map(mf));
// enumFromTo :: Enum a => a -> a -> [a]
const enumFromTo = (m, n) =>
(typeof m !== 'number' ? (
enumFromToChar
) : enumFromToInt)
.apply(null, [m, n]);
// enumFromToInt :: Int -> Int -> [Int]
const enumFromToInt = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// EXAMPLE ----------------------------------------------------------------
// [(x, y, z) | x <- [1..n], y <- [x..n], z <- [y..n], x ^ 2 + y ^ 2 == z ^ 2]
return bind(enumFromTo(1, n),
x => bind(enumFromTo(x, n),
y => bind(enumFromTo(y, n),
z => x * x + y * y === z * z ? [
[x, y, z]
] : []
)));
})(20);

View file

@ -0,0 +1 @@
[[3, 4, 5], [5, 12, 13], [6, 8, 10], [8, 15, 17], [9, 12, 15], [12, 16, 20]]

View file

@ -0,0 +1,7 @@
# #camlp4o;;
# #require "camlp4.listcomprehension";;
/home/user//.opam/4.06.1+trunk+flambda/lib/ocaml/camlp4/Camlp4Parsers/Camlp4ListComprehension.cmo: loaded
# [ x * 2 | x <- [1;2;3;4] ];;
- : int list = [2; 4; 6; 8]
# [ x * 2 | x <- [1;2;3;4]; x > 2 ];;
- : int list = [6; 8]

View file

@ -0,0 +1 @@
[(x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z]

View file

@ -0,0 +1 @@
((x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z)

View file

@ -0,0 +1,5 @@
def triplets(n):
for x in xrange(1, n + 1):
for y in xrange(x, n + 1):
for z in xrange(y, n + 1):
yield x, y, z

View file

@ -0,0 +1 @@
[(x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2]

View file

@ -0,0 +1 @@
((x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2)

View file

@ -1,17 +1,17 @@
/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/
parse arg n . /*get the optional argument from the CL*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n=100 /*Not specified? Then use the default.*/
say 'Pythagorean triples (a² + b² = c², c ' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/
do c=b+1 to n
if aabb==c*c then $=$ '{'a"," || b','c"}"
end /*c*/
end /*b*/
end /*a*/
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */
do b=a+1 to n-1; aabb=aa + b*b /* ··· not by much.*/
do c=b+1 to n
if aabb==c*c then $=$ '{'a"," || b','c"}"
end /*c*/
end /*b*/
end /*a*/
#=words($)
do j=1 for #
say left('', 20) word($, j) /*display a member of the list, */
end /*j*/ /* [↑] list the members vertically. */
do j=1 for #
say left('', 20) word($, j) /*display a member of the list, */
end /*j*/ /* [↑] list the members vertically. */
say # 'members listed.' /*stick a fork in it, we're all done. */

View file

@ -3,13 +3,13 @@ parse arg n . /*get the optional argument fro
if n=='' | n=="," then n=100 /*Not specified? Then use the default.*/
say 'Pythagorean triples (a² + b² = c², c ' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/
do c=b+1 to n
if aabb==c*c then $=$ '{'a"," || b','c"}"
end /*c*/
end /*b*/
end /*a*/ /*stick a fork in it, we're all done. */
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */
do b=a+1 to n-1; aabb=aa + b*b /* ··· not by much.*/
do c=b+1 to n
if aabb==c*c then $=$ '{'a"," || b','c"}"
end /*c*/
end /*b*/
end /*a*/ /*stick a fork in it, we're all done. */
#=words($) /*number of members in the list. */
say; say strip($) /*show the Pythagorean triples to term.*/
say; say # 'members listed.' /*triples are listed in order of 1st #.*/
say; say strip($) /*show the Pythagorean triples to term.*/
say; say # 'members listed.' /*triples are listed in order of 1st #.*/

View file

@ -0,0 +1,8 @@
function grid(n,p) {
return(colshape(J(p,1,1..n)',1),J(n,1,1::p))
}
n = 20
a = grid(n,n)
a = a,sqrt(a[.,1]:^2+a[.,2]:^2)
a[selectindex(floor(a[.,3]):==a[.,3] :& a[.,3]:<=n),]