This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ == recursive version == | |
: fib | |
dup 2 <= if | |
drop 1 | |
else | |
dup 1 - recurse | |
swap 2 - recurse + | |
then ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/home/user/localperl/bin/perl | |
use v5.20; | |
use strict; | |
use warnings; | |
use feature qw| say signatures |; | |
my %multi; | |
sub def ($name, $param, $lambda) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use v6; | |
# overloading string 'x' operator | |
multi infix:<x> (Int \a, Int \b) { a * b } | |
# but we still want the string 'x' operator | |
multi infix:<x> (Str \a, Int \b where * > 1) { [~] gather for ^b { take a } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- defac as multi-dispatch pseudo-code | |
-- | |
-- defac(x): defac(x / 1, 1) [ where x is Integer ] | |
-- defac(x, d): d - x [ where x = 1 ] | |
-- defac(x, d): error() [ where x < 1 ] | |
-- defac(x, d): defac(x / d, d + 1) [ where x > 1 ] | |
function defac (x, d) | |
d = d or 1 | |
if x == 1 then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- as multi-dispatch pseudo-code | |
-- | |
-- range(init, final, step): | |
---- return | |
---- [ where init > final ] | |
-- | |
-- range(final): | |
---- return 0, range(0 + 1, final, 1) | |
---- [ where final is Positive Integer ] | |
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use v6; | |
multi fib(Int $n where -> $n { $n ~~ any(1, 2) }) { 1 } | |
multi fib(Int $n where -> $n { $n > 2 }) { fib($n - 1) + fib($n - 2) } | |
say fib 10; # 55 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// max & min size of strings | |
#define MIN 50 | |
#define MAX 500 | |
// limit of messages | |
#define MSGS 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~~~ | n + 1 :@: if m = 0 ~~~ # | |
# ~~~ A(m, n) = | A(m - 1, 1) :@: if m > 0 and n = 0 ~~~ # | |
# ~~~ | A(m - 1, A(m, n - 1) :@: if m > 0 and n > 0 ~~~ # | |
multi A($m where(* == 0), $n ) { $n + 1 } | |
multi A($m where(* > 0), $n where(* == 0)) { A($m - 1, 1) } | |
multi A($m where(* > 0), $n where(* > 0)) { A($m - 1, A($m, $n - 1)) } | |
say A(1, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use v6; | |
multi chain ([ ]) { [ head => [ ] ] } | |
multi chain (@Ls) { [ head => @Ls.shift, tail => chain @Ls ] } | |
# test | |
my @chained = chain [ 1, 2, 3 ]; | |
say @chained.perl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- if and else as methods (test) -- | |
-- object -- | |
local logic = { main = nil } | |
-- if method -- | |
function logic: so(code) | |
if self.main then | |
code( ) | |
end |