summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2022-12-04 12:54:29 +0200
committerIgor Pashev <pashev.igor@gmail.com>2022-12-04 12:54:29 +0200
commit64abe133c1880bc0dd5bce5bc67c94bf2f083467 (patch)
tree6faeac8e697e261a97be725f139d33c5a1cd3b1f
parent8ee53fb45b407a2035d33895bcc5c4b44b0b70e9 (diff)
downloadgcd-64abe133c1880bc0dd5bce5bc67c94bf2f083467.tar.gz
Add APL
-rwxr-xr-xgcd.apl46
1 files changed, 46 insertions, 0 deletions
diff --git a/gcd.apl b/gcd.apl
new file mode 100755
index 0000000..3c0060d
--- /dev/null
+++ b/gcd.apl
@@ -0,0 +1,46 @@
+#!/usr/bin/apl --script
+⍝
+⍝ Tested with GNU APL 1.8 (2022-10-25)
+⍝
+⍝ Synopsis:
+⍝ $ ./gcd.apl -- 11 22 121
+⍝ or:
+⍝ $ apl --script [-f] gcd.apl -- 11 22 121
+⍝
+⍝ One-liner, where "∨" is APL's built-in GCD:
+⍝ $ apl --eval "∨/⍎¨(↑⍸{'--'≡⍵}¨a)↓a←⎕ARG" -- 11 22 121
+⍝
+
+⍝ Function to get the script arguments,
+⍝ that is everything after '--':
+∇ r ← sargs; a
+ a ← ⎕ARG
+ r ← (↑⍸{'--'≡⍵}¨a)↓a
+∇
+
+⍝ Function to calculate the GCD of two numbers:
+∇ r ← a gcd2 b
+T: →(b=0)/E ⋄ (a b) ← b (b|a) ⋄ →T
+E: r ← a
+∇
+
+⍝ Recursive version:
+⍝ ∇ r ← a gcd2 b
+⍝ r ← a
+⍝ →(b=0)/0
+⍝ r ← b gcd2 b|a
+⍝ ∇
+
+⍝ Function (lambda) to calculate the GCD of several numbers:
+gcdn ← {gcd2/⍵}
+
+∇ main; a
+ a ← sargs ⍝ Get script arguments
+ →(0=≢a)⍴0 ⍝ Exit the function if no arguments
+ ⎕ ← gcdn⍎¨a ⍝ Convert strings to numbers, calculate and print GCD
+∇
+
+main
+
+)OFF
+