blob: 3c0060d635a0ae03c564e13b73174fd497450e49 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
|