summaryrefslogtreecommitdiff
path: root/gemfeed
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2022-04-23 10:02:03 +0100
committerPaul Buetow <paul@buetow.org>2022-04-23 10:02:03 +0100
commitf4de32f89b7b9921259b405a1098a061eac5e314 (patch)
tree38c0ee0a69d32f062eaed3298a4eb48748411cf4 /gemfeed
parent365a558bef08b0de47a511e6270230723a9ef8db (diff)
Also runs with C++ and Raku
Diffstat (limited to 'gemfeed')
-rw-r--r--gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi103
1 files changed, 68 insertions, 35 deletions
diff --git a/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi b/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi
index d6da6f5a..3fe85a38 100644
--- a/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi
+++ b/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.gmi
@@ -1,6 +1,6 @@
-# The fibonacci.pl.c Polyglot
+# The fibonacci.pl.raku.c Polyglot
-> Published by Paul at 2014-03-24
+> Published by Paul at 2014-03-24, last updated 2022-04-23
In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it.
@@ -8,7 +8,7 @@ In computing, a polyglot is a computer program or script written in a valid form
## The Fibonacci numbers
-For fun, I programmed my own Polyglot, which is both valid Perl and C code. The exciting part about C is that $ is a valid character to start variable names with:
+For fun, I programmed my own Polyglot, which is both valid Perl, Raku, C and C++ code (I have added C++ and Raku support in 2022). The exciting part about C and C++ is that $ is a valid character to start variable names with:
```
#include <stdio.h>
@@ -21,37 +21,36 @@ For fun, I programmed my own Polyglot, which is both valid Perl and C code. The
my $arg;
sub hello() {
- printf("Hello, welcome to Perl-C!\n");
- printf("This program is both valid C and Perl code!\n");
- printf("It calculates all fibonacci numbers from 0 to 9!\n\n");
- return 0;
+ printf("Hello, welcome to the Fibonacci Numbers!\n");
+ printf("This program is all, valid C and C++ and Perl and Raku code!\n");
+ printf("It calculates all fibonacci numbers from 0 to 9!\n\n");
+ return 0;
}
sub fibonacci() {
- my $n = $arg;
+ my $n = $arg;
- if ($n < 2) {
- return $n;
- }
+ if ($n < 2) {
+ return $n;
+ }
- $arg = $n - 1;
- my $fib1 = fibonacci();
- $arg = $n - 2;
- my $fib2 = fibonacci();
+ $arg = $n - 1;
+ my $fib1 = fibonacci();
+ $arg = $n - 2;
+ my $fib2 = fibonacci();
- return $fib1 + $fib2;
+ return $fib1 + $fib2;
}
BEGIN {
- hello();
- my $i = 0;
-
- for ($i = 0; $i <= 10; ++$i) {
- $arg = $i;
- printf("fib(%d) = %d\n", $i, fibonacci());
- }
-
- return 0;
+ hello();
+ my $i = 0;
+
+ while ($i <= 10) {
+ $arg = $i;
+ printf("fib(%d) = %d\n", $i, fibonacci());
+ $i++;
+ }
}
```
@@ -59,12 +58,13 @@ You can find the full source code at GitHub:
=> https://codeberg.org/snonux/perl-c-fibonacci
-### Let's run it with Perl:
+### Let's run it with C and C++
```
-❯ perl fibonacci.pl.c
-Hello, welcome to Perl-C!
-This program is both valid C and Perl code!
+% gcc fibonacci.pl.raku.c -o fibonacci
+% ./fibonacci
+Hello, welcome to the Fibonacci Numbers!
+This program is all, valid C and C++ and Perl and Raku code!
It calculates all fibonacci numbers from 0 to 9!
fib(0) = 0
@@ -78,16 +78,49 @@ fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
-```
+% g++ fibonacci.pl.raku.c -o fibonacci
+% ./fibonacci
+Hello, welcome to the Fibonacci Numbers!
+This program is all, valid C and C++ and Perl and Raku code!
+It calculates all fibonacci numbers from 0 to 9!
-### Let's compile it as C and run the binary:
+fib(0) = 0
+fib(1) = 1
+fib(2) = 1
+fib(3) = 2
+fib(4) = 3
+fib(5) = 5
+fib(6) = 8
+fib(7) = 13
+fib(8) = 21
+fib(9) = 34
+fib(10) = 55
+```
+
+### Let's run it with Perl and Raku
```
-❯ gcc fibonacci.pl.c -o fibonacci
-❯ ./fibonacci
-Hello, welcome to Perl-C!
-This program is both valid C and Perl code!
+% perl fibonacci.pl.raku.c
+Hello, welcome to the Fibonacci Numbers!
+This program is all, valid C and C++ and Perl and Raku code!
+It calculates all fibonacci numbers from 0 to 9!
+
+fib(0) = 0
+fib(1) = 1
+fib(2) = 1
+fib(3) = 2
+fib(4) = 3
+fib(5) = 5
+fib(6) = 8
+fib(7) = 13
+fib(8) = 21
+fib(9) = 34
+fib(10) = 55
+
+% raku fibonacci.pl.raku.c
+Hello, welcome to the Fibonacci Numbers!
+This program is all, valid C and C++ and Perl and Raku code!
It calculates all fibonacci numbers from 0 to 9!
fib(0) = 0