Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Nial
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == === Creating arrays === count 6 |1 2 3 4 5 6 Arrays can also be literal Arr := [5, 6, 7, 8, 9] |5 6 7 8 9 Shape gives the array dimensions and reshape can be used to reshape the dimensions. shape Arr |5 a := 2 3 reshape Arr # reshape is a binary operation with two arguments. It can also be written in prefix as # a := reshape [[2,3], Arr] |5 6 7 |8 9 5 b := 3 2 reshape Arr |5 6 |7 8 |9 5 {{not a typo|a}} inner[+,*] b |130 113 |148 145 === Computing an average === Definitions are of the form '<name> is <expression>' average is / [sum, tally] average Arr |7. === Computing a factorial === fact is recur [ 0 =, 1 first, pass, product, -1 +] fact 4 |24 === Reversing an array === rev is reshape [ shape, across [pass, pass, converse append ] ] rev [1, 2, 3, 4] |4 3 2 1 === Generating primes === Contrast with [[APL programming language|APL]] primes is sublist [ each (2 = sum eachright (0 = mod) [pass,count]), pass ] rest count primes 10 |2 3 5 7 ==== Explanation ==== Checking the divisibility of A by B is_divisible is 0 = mod [A,B] Defining is_prime filter is_prime is 2 = sum eachright is_divisible [pass,count] Count generates an array [1..N] and pass is N (identity operation). eachright applies is_divisible (pass, element) in each element of count-generated array. Thus this transforms the count-generated array into an array where numbers that can divide N are replaced by '1' and others by '0'. Hence if the number N is prime, sum [transformed array] must be 2 (itself and 1). Now all that remains is to generate another array using count N, and filter all that are not prime. primes is sublist [each is_prime, pass] rest count === QuickSort === * <code>link</code> joins its argument arrays * <code>sublist</code> [A,B] returns a list of items of B chosen according to the list of booleans given in A, selecting those items of B where the corresponding item of A is true. * In a <code>Fork</code> [A,B,C] X the first A is a predicate, and if A(X) is true, then B(X) is returned else C(X) is returned. * <code>Pass</code> is an identity operation for arrays. quicksort is fork [ >= [1 first,tally], pass, link [ quicksort sublist [ < [pass, first], pass ], sublist [ match [pass,first],pass ], quicksort sublist [ > [pass,first], pass ] ] ] Using it: quicksort [5, 8, 7, 4, 3] |3 4 5 7 8
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)