Re: Adding 2 + 2
by I0 (Priest) on Dec 31, 2000 at 21:36 UTC
|
package Conway;
use overload
'+' => \&Add,
'-' => \&Sub,
'*' => \&Mul,
'>=' => \&Geq,
'<=' => \&Leq,
'==' => \&eql,
'<=>' => \&Cmp,
'neg' => \&Neg,
'++' => \&Succ,
'""' => \&Name,
'0+' => \&Value,
'=' => \&Copy,
bool => sub{my $x = shift; return !Eql($x,$zero)},
;
sub Geq{
my($x,$y) = @_;
my @xL = @{$x->[0]};
my @xR = @{$x->[1]};
my @yL = @{$y->[0]};
my @yR = @{$y->[1]};
return !(grep{Leq($_,$y)}@xR) && !(grep{Leq($x,$_)}@yL);
}
sub Leq{
my($x,$y) = @_;
return Geq($y,$x);
}
sub Eql{
my($x,$y) = @_;
return Geq($x,$y) && Geq($y,$x)
}
sub Gtr{
my($x,$y) = @_;
return Geq($x,$y) && !Geq($y,$x);
}
sub Cmp{
my($x,$y) = @_;
my $g = Geq($x,$y);
my $l = Leq($x,$y);
return undef unless $g || $l;
return 0 if $g && $l;
return 1 if $g && !$l;
return -1 if !$g && $l
}
sub Neg{
my $x = shift;
my @xL = @{$x->[0]};
my @xR = @{$x->[1]};
return bless [
[map{Neg($_)}@xR],
[map{Neg($_)}@xL]
];
}
sub Add{
my($x,$y) = @_;
my @xL = @{$x->[0]};
my @xR = @{$x->[1]};
my @yL = @{$y->[0]};
my @yR = @{$y->[1]};
bless[
[
(map{Add($_,$y)} @xL),
(map{Add($x,$_)} @yL),
],
[
(map{Add($_,$y)} @xR),
(map{Add($x,$_)} @yR),
]
];
}
sub Sub{
my($x,$y) = @_;
Add($x,Neg($y));
}
sub dmap(&$$){
my $f = shift;
my @a = @{shift @_};
my @b = @{shift @_};
map{
local $a = $_;
map{ local $b=$_; &$f } @b;
} @a;
}
sub Mul{
my($x,$y) = @_;
my @xL = @{$x->[0]};
my @xR = @{$x->[1]};
my @yL = @{$y->[0]};
my @yR = @{$y->[1]};
bless [
[
(dmap{Sub(Add(Mul($a,$y),Mul($x,$b)),Mul($a,$b))} \@xL,\@yL),
(dmap{Sub(Add(Mul($a,$y),Mul($x,$b)),Mul($a,$b))} \@xR,\@yR),
],
[
(dmap{Sub(Add(Mul($a,$y),Mul($x,$b)),Mul($a,$b))} \@xL,\@yR),
(dmap{Sub(Add(Mul($a,$y),Mul($x,$b)),Mul($a,$b))} \@xR,\@yL),
]
]
}
sub L{
my $x = shift;
return @{$x->[0]};
};
sub R{
my $x = shift;
return @{$x->[1]};
}
sub normalize{
my($x,$y) = @_;
my @xL = map{normalize($_)} @{$x->[0]};
my @xR = map{normalize($_)} @{$x->[1]};
while( @xR > 1 ){
if( Geq(@xR[0],@xR[-1]) ){
shift @xR;
}elsif( Geq(@xR[-1],@xR[0]) ){
pop @xR;
}else{
warn "NaN"; last;
}
}
while( @xL > 1 ){
if( Geq(@xL[0],@xL[-1]) ){
pop @xL;
}elsif( Geq(@xL[-1],@xL[0]) ){
shift @xL;
}else{
warn "NaN"; last;
}
}
bless [[@xL],[@xR]];
}
sub Succ{
my $x = shift;
bless [[$x],[]];
}
sub Copy{
my $x = shift;
normalize($x);
}
sub new{
my $class = shift;
my ($L, $R) = @_;
my $n = [$L,$R];
bless $n,$class;
return $n;
}
sub Name{
my $x = shift;
# return 0 if Eql($x,$zero);
my @xL = @{$x->[0]};
my @xR = @{$x->[1]};
my $L=join",",map{Name($_)} @xL;
my $R=join",",map{Name($_)} @xR;
return "{$L|$R}";
}
sub Value{
my $x = normalize(shift);
my $g = Geq($x,$zero);
my $l = Leq($x,$zero);
return NaN unless $g || $l;
return 0 if $g && $l;
return -Value(Neg($x)) if $l && !$g;
return 1+Value(Sub($x,$one) ) if( Geq($x,$one));
return Value( Mul($x,$two) )/2;
}
$zero = new Conway [],[];
$one = $zero->Succ;
$two = $one+$one;
#print "$one+$one=$two\n";
#print Value($one),"+",Value($one),"=",Value($two),"\n";
$two = normalize($two);
#print "$one+$one=$two\n";
#print Value($one),"+",Value($one),"=",Value($two),"\n";
print "$two+$two=",$two+$two,"\n";
print Value($two),"+",Value($two),"=",Value($two+$two),"\n";
#print Value(Mul($two,$two)),"\n";
#print Value([[$zero],[$one]]),"\n";
1;
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
RE: Adding 2 + 2
by and (Pilgrim) on Sep 12, 2000 at 01:11 UTC
|
One lunch I was complaining about how or and
die get all of the good work and some of my
bit-twiddling coworkers said that they hardly ever have
anything to do. We went out for drinks later and they
tried to teach me what they know about integers. I find
it very hard to understand but I think I figured out how
to do 2+2.
sub add {
my( $a, $b, $bit, $cary, $sum ) = ( @{$_[0]}, 1, 0, 0 );
$sum |= $bit if 1 & ( $a ^ $b ^ $cary );
$cary = 1 & ( $a & $b | ( $cary & ($a|$b) ) );
return $a | $b ? [$a>>1,$b>>1,$bit<<1,$cary,$sum] : $sum;
}
my @args = map {0+$_} (@ARGV,2,2)[0,1];
my $acc = add( [@args] );
$acc = add($acc) while ref $acc;
print "add[@args] = $acc\n";
| [reply] [Watch: Dir/Any] [d/l] [select] |
RE: Adding 2 + 2
by KM (Priest) on Sep 11, 2000 at 19:01 UTC
|
perl -MFcntl=:flock -e 'print 2+LOCK_EX'
Or (cheap way)
perl -wle 'print $^W*2**$^W*2'
Or (Only works if you follow the directions ;-)
#!/usr/bin/perl -w
use strict;
print "Pick an integer between 1 and 3\n";
my $num = <STDIN>;
print "Pick another integer between 1 and 3\n";
$num += <STDIN>;
print "The sum is " . $num;
Or
perl -wle '%h = map {(qq{sum}) => $h{'sum'}+=$_ } qw/2 2/; print $h{s
+um}'
Or (a long version of the above one liner)
#!/usr/bin/perl -w
use strict;
my %foo;
%foo = map {('sum') => $foo{'sum'}+=$_ } qw/2 2/;
print $foo{sum};
More later if I have time.
Cheers,
KM | [reply] [Watch: Dir/Any] [d/l] [select] |
|
I follow the directions, but it tells me that the
sum is 3. Did you mean to write directions that do not
allow 1.5 as input?
| [reply] [Watch: Dir/Any] |
|
Haha.. I made the directions more clear.
Cheers,
KM
| [reply] [Watch: Dir/Any] |
|
|
|
|
perl -wle 'print $^W*2**$^W*2'
That's not 2 + 2, thats 2 squared. (Ok, I know... the answer is the same... but the route is different.) | [reply] [Watch: Dir/Any] [d/l] |
|
Well, speaking as a engineer by training, the program meets spec... where's the problem? <G>
If you wanted flexibility and easy upgrades, you should have made sure that was part of the original program spec.
Ozymandias pries tongue from cheek...
- email Ozymandias
| [reply] [Watch: Dir/Any] |
|
Uh, I thought 2 squared was just a shorthand for 2 + 2,
er, or inherently linked or something. Sure, it a ...
what's that word, pathological? case but ...
a
| [reply] [Watch: Dir/Any] |
|
|
*bends down on knee*
Will you marry me? ;)
| [reply] [Watch: Dir/Any] |
RE: Adding 2 + 2
by t0mas (Priest) on Sep 11, 2000 at 10:13 UTC
|
Q: How many ways are there?
A: TMTOWTDI
$_="2+2";
print $_."=".eval;
/brother t0mas
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
RE: Adding 2 + 2
by Adam (Vicar) on Sep 11, 2000 at 08:23 UTC
|
$,=2+2=>print('The result is ',$/);
| [reply] [Watch: Dir/Any] [d/l] |
(ar0n) RE: Adding 2 + 2
by ar0n (Priest) on Sep 11, 2000 at 11:25 UTC
|
#!/usr/bin/perl -w
use strict;
my $i = 3;
my $j = $i-1;
my $n = $i & $j;
print "$n + $n = ", $n + $n;
-- ar0n, i broke my 'two' key :-(
| [reply] [Watch: Dir/Any] [d/l] |
(jcwren) RE: Adding 2 + 2
by jcwren (Prior) on Sep 11, 2000 at 07:38 UTC
|
#!/usr/local/bin/perl -w
use strict;
{
my $addy = "2 + 2";
print "Adding $addy = ", eval ($addy), "\n";
}
It's late, it's time for bed, and I couldn't make Inline work in 30 seconds, so I cheated...
--Chris
e-mail jcwren | [reply] [Watch: Dir/Any] [d/l] |
|
OK, there's the challenge:
use Inline C => 'int add(int a, int b) { return a + b; }';
print add(2,2), "\n";
Hmm. Maybe I should hoist this up as a real snippet. {grin}
-- Randal L. Schwartz, Perl hacker | [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by Adam (Vicar) on Sep 11, 2000 at 21:00 UTC
|
Ok, I have a few more. First, the one I posted last night followed by several new ones. (Including one that uses no digits)
perl -we "$,=2+2=>print('The result is ',$/)"
perl -we "1while(2+2>++$.);print qq;The result is $.$/;"
perl -we "tie $sum,'_',4;print 'The result is ',$sum,$/;sub _::TIESCAL
+AR{bless[pop],'_'}sub _::FETCH{$_[0][0]=2+2}"
perl -we "my $two=$^W+!''; print 'The result is ',$two + $two,$/"
perl -we "print reverse $/,eval(join'+',(2)x2),'The result is '"
perl -we "$_=qq/The result is 2+2\n/=>y/2\+2/4/s,print"
I can only get away with the last one because Ozymandias says I can. | [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by myocom (Deacon) on Sep 12, 2000 at 00:53 UTC
|
print 4;
Hey, it meets spec too! | [reply] [Watch: Dir/Any] [d/l] |
(jcwren) RE: Adding 2 + 2
by jcwren (Prior) on Sep 11, 2000 at 21:26 UTC
|
#!/usr/local/bin/perl -w
use strict;
use Lingua::EN::Numbers qw(American);
{
my $v++;
my $t=new Lingua::EN::Numbers(++$v);
my $f=new Lingua::EN::Numbers($v*$v);
print sprintf "Adding %s + %s = %s\n",$t->get_string,$t->get_string
+,$f->get_string;
}
--Chris
e-mail jcwren | [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by jeorgen (Pilgrim) on Sep 11, 2000 at 19:49 UTC
|
my $four;
my @array = split "", (2 x 2);
sub the {
my $four = shift;
return "two plus two is: $four"}
sub answer {
print shift}
sub result {
return $four}
foreach my $two (@array) {
$four += $two
}
answer the result;
/jeorgen | [reply] [Watch: Dir/Any] [d/l] |
Re: Adding 2 + 2
by mkmcconn (Chaplain) on Feb 11, 2001 at 03:51 UTC
|
2+2 illustrates some interesting behavior.
- ++$_ && $_++ && print eval() + eval();
- $_++; print eval() + eval() unless ( $_ + 1 == $_++ );
- for (2..2){$_ = $_ + $_ and print;}
- $a = 2; print "$a + $a = ", $$b + $$b; BEGIN{ $b = \$a }
- my $num = 2; $num =~ s/(2)/$1 + $1/gxe; print $num;
mkmcconn | [reply] [Watch: Dir/Any] [d/l] [select] |
(crazyinsomniac) RE: Adding 2 + 2
by crazyinsomniac (Prior) on Sep 11, 2000 at 06:38 UTC
|
print "Adding 2 + 2 =".(2+2)."\n";
"cRaZy is co01, but sometimes cRaZy is cRaZy".
- crazyinsomniac | [reply] [Watch: Dir/Any] [d/l] |
Re: Adding 2 + 2
by I0 (Priest) on Dec 30, 2000 at 18:33 UTC
|
sub S{ return [shift]; }
sub P{ return (shift)->[0]; }
sub add{
my($x,$y) = @_;
return $x unless $y;
return add(S($x),P($y));
}
sub arabic{
my $x = shift;
return 0 unless $x;
return 1+arabic(P($x));
}
$y=$x = S S;
print arabic($x),"+",arabic($y),"=",arabic(add($x,$y)),"\n";
| [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by BastardOperator (Monk) on Sep 11, 2000 at 17:11 UTC
|
Only on PerlMonks....only here would you find this much attention given to adding 2 + 2 ;). I got a real chuckle out of this | [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2 (stack machine)
by ikegami (Patriarch) on Jan 30, 2007 at 18:10 UTC
|
my @stack;
my $acc = 0;
sub PUSH() { push(@stack, $acc); }
sub POP() { pop(@stack); }
sub INC() { $acc++; }
sub ADD() { $acc += POP; }
INC;
INC;
PUSH;
ADD;
print($acc, "\n");
| [reply] [Watch: Dir/Any] [d/l] |
|
The parent assumed the accumulator was initially zero. That's not likely in a real-life scenario, so this removes the assumption:
my @stack;
my $acc = rand;
sub PUSH() { push(@stack, $acc); }
sub POP() { pop(@stack); }
sub INC() { $acc++; }
sub ADD() { $acc += POP; }
sub SUB() { $acc -= POP; }
PUSH;
SUB;
INC;
INC;
PUSH;
ADD;
print("$acc\n");
| [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by mirod (Canon) on Sep 11, 2000 at 21:43 UTC
|
unless( my $pid= open( STDOUT, "|-"))
{ while( <STDIN>)
{ chomp; s/(.*)/qq{$1}. "= " . eval($1) ."\n"/e; print; }
exit;
}
print "2+2\n";
| [reply] [Watch: Dir/Any] [d/l] |
(jcwren) RE: Adding 2 + 2
by jcwren (Prior) on Sep 11, 2000 at 21:44 UTC
|
The Doesn't completely use Perl solution. Last one, I promise.
#!/usr/local/bin/perl -w
use strict;
{
my $v=2;
print "Adding $v + $v = ", `echo $v $v + p | dc`;
}
(This should even work on Win32 with the cygwin stuff)
--Chris
e-mail jcwren | [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by d4vis (Chaplain) on Sep 12, 2000 at 02:32 UTC
|
@list=(1,2,3);
if ($list[1] eq '2' && $list[2]++ eq '3') {
print "The answer is $list[2]\n";
} else {
print "Insufficiently large value of 2\n";
}
~d4vis
#!/usr/bin/fnord | [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by rodry (Beadle) on Sep 11, 2000 at 10:09 UTC
|
And I though I had a lot of free time. | [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2
by mr_mischief (Monsignor) on Dec 21, 2001 at 14:21 UTC
|
I didn't notice anyone using Roman numerals. I tested the applicable subroutine with lots of values, since I wrote it for this and hadn't used it before. If there are any bugs with it other than the design decision documented, please let me know. This might actually sneak into a project since I spent a good half hour designing, coding, testing, and commenting it.
Chris
| [reply] [Watch: Dir/Any] [d/l] |
|
A very creative solution :)
Be sure to check out Roman if you didn't know it already. It can convert roman numbers both ways, and checks if your roman input is valid.
use Roman;
$calc = "II + II";
$calc =~ s/([MDCLXVI]+)/isroman($1) ? arabic($1) : $1/;
print eval $calc;
2;0 juerd@ouranos:~$ perl -e'undef christmas'
Segmentation fault
2;139 juerd@ouranos:~$
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Thanks for the review and the suggestion.
I knew Roman was available, but it didn't feel right to use it for this. I felt it was more in the spirit of the thread to go overboard and develop the needed tools specifically for the node. I'm actually a bit surprised it took someone posting a hand-crafted Roman to Arabic numeral conversion before someone gave an example using the module. It was one of the first twists on the problem that popped into my deranged mind.
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
I didn't know about the contest until just now. Thanks for asking, though. I'm enjoying looking at the site.
| [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2
by ikegami (Patriarch) on Jul 22, 2005 at 15:47 UTC
|
$lterm = 2; # lterm = 2
$rterm = 2; # rterm = 2
$#add = $lterm; pop(@add); # add = lterm
push(@acc, @add); # acc += add
$#add = $rterm; pop(@add); # add = rterm
push(@acc, @add); # acc += add
push(@acc, 1); $sum = $#acc; # sum = acc
print("$sum\n");
It must be fast, since the addition is done by push, which is written in C! ;) | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by mrborisguy (Hermit) on Jul 24, 2005 at 02:21 UTC
|
use Language::Haskell;
print Language::Haskell->new->eval('foldr (+) 0 [2,2]');
(Hey, I saw somebody do it with C, so I can do it in Haskell)
use Language::Functional qw(foldr);
print foldr { $_[0] + $_[1] } 0, [2,2];
-Bryan | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by arkturuz (Curate) on Jan 31, 2007 at 09:44 UTC
|
And now for something completely different:
say 2+2;
P.S. I've been waiting so long to put those Monty Pythons' words on the stage, I now I finally got it :-)
| [reply] [Watch: Dir/Any] [d/l] |
Re: Adding 2 + 2 (Church numerals)
by blokhead (Monsignor) on Jan 30, 2007 at 18:43 UTC
|
Lambda calculus! But it doesn't have integers (or any data types for that matter), you say? Sure it does, it has Church numerals!
my $two = sub { my($f,$x) = @_; $f->($f->($x)); };
sub add {
my($m,$n) = @_;
sub { my($f,$x) = @_; $m->($f, $n->($f, $x)) };
}
sub decode { my($n) = @_; $n->( sub{$_[0]+1}, 0 ); }
print decode( add($two, $two) ), $/;
Yeah, at some point we have to convert the Church numeral to something that Perl can print.. but you get the idea.
| [reply] [Watch: Dir/Any] [d/l] |
RE: Adding 2 + 2
by Anonymous Monk on Sep 26, 2000 at 02:00 UTC
|
(undef$a)and(undef$b)or++$a;while(
$a!=2){$a++if($b);$b++if(not$b);}
$b++?$d=$a.'+'.$b.'=':die;map{$c+=
$_;}($a,$b);$_=$d.$c."\n";print; | [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2
by GrandFather (Saint) on Jan 29, 2007 at 19:57 UTC
|
$|++;
my $two = $| + $|;
print "$two + $two = ", $two + $two;
Prints:
2 + 2 = 4
DWIM is Perl's answer to Gödel
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
my $one = !@!;
my $one = !!$];
my $one = /.*/; # Side-effect: Changes $1, etc
my $one = $$/$$;
my $one = ()=$$;
my $one = @{[$$]};
my $one = 'a'^'P'; # Character set specific.
my $one = ord('b')-ord('a'); # Character set specific.
And 2:
my $two = $one+$one;
my $two = $one<<$one;
my $two = ()=qw(a a);
my $two = @{[ qw(a a) ]};
my $two = ()=/(.*)(.*)/; # Side-effect: Changes $1, etc
my $two = 'a'^'S'; # Character set specific.
my $two = ord('c')-ord('a'); # Character set specific.
None of these have side effects unless otherwise noted.
All of these run under use strict;.
All of these run silently under use warnings;.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
my $one = !$|;
my $one = cos; # Given that $_ == 0.
my $one = cos $|;
my $one = cos atan2 $|, cos;
my $one = sin atan2 sin, $|;
my $one = length $/; # Note: possibly 2 under Windows or DOS.
my $one = length $";
my $one = length $;;
my $one = length $^L;
my $one = $^T/$^T;
my $one = @ARGV >= 0;
my $one = grep //, ($_);
my $one = !undef;
# Note: the following may be Unix-only. Will not work if run as root,
# as root's UID and GID are (always?) zero.
my $one = $</$<; # User ID.
my $one = $>/$>; # Effective user ID.
my $one = $(/$(; # (Space-separated list of) group ID(s).
my $one = $)/$); # (Space-separated list of) effective group ID(s
+).
Ways to get 2:
my $two = length $:;
my $two = $^F; # Note: system-dependent.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by roboticus (Chancellor) on Jan 31, 2007 at 03:53 UTC
|
At first, I tried this simple recursive algorithm:
#!/usr/bin/perl -w
use strict;
use warnings;
sub add {
my ($first, @rest) = @_;
if (!defined $first) {
0;
}
else {
die "operand may not be negative!" unless $first >= 0;
if ($first == 0) {
add(@rest);
}
else {
1 + add($first-1, @rest);
}
}
}
sub print_add {
print join(" + ", @_), " = ", add(@_), "\n";
}
print_add 2,2;
print_add 2,2,2;
print_add 4,8,12;
which yields the results:
root@swill ~/PerlMonks
$ ./adder_1.pl
2 + 2 = 4
2 + 2 + 2 = 6
4 + 8 + 12 = 24
But I then realized that the algorithm used is essentially tail-recursive. Thus, it can be optimized! Converting the add routine from tail-recursive form to an iterative form yields:
sub add {
my $accumulator = 0;
for my $op (@_) {
for my $i (1 .. $op) {
$accumulator = $accumulator+1;
}
}
$accumulator;
}
Now all that remained is to verify that the conversion was worthwhile:
#!/usr/bin/perl -w
use strict;
use warnings;
use Benchmark qw(timethese cmpthese);
sub add_rec {
my ($first, @rest) = @_;
if (!defined $first) {
0;
}
else {
die "operand may not be negative!" unless $first >= 0;
if ($first == 0) {
add_rec(@rest);
}
else {
1 + add_rec($first-1, @rest);
}
}
}
sub add_iter {
my $accumulator = 0;
for my $op (@_) {
for my $i (1 .. $op) {
$accumulator = $accumulator+1;
}
}
$accumulator;
}
cmpthese(100000, {
'Recursive' => sub { 2 == add_rec(2,2); },
'Iterative' => sub { 2 == add_iter(2,2); }
});
Running the benchmarking program shows:
$ ./adder_bench.pl
Rate Recursive Iterative
Recursive 79051/s -- -48%
Iterative 152207/s 93% --
Success!
The iterative version is *much* faster than the recursive one. I'm certain that this new algorithm for adding should be used in all future programs, as the iterative version is better both in speed (it's nearly twice as fast!) as well as consuming far less memory than it's recursive counterpart when adding large numbers.
--roboticus
Awaiting his Turing prize for this valuable discovery... | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by Errto (Vicar) on Jul 24, 2005 at 05:54 UTC
|
A simple one that didn't make it in yet:
print "2 + 2 = ${ \(2 + 2) }\n";
| [reply] [Watch: Dir/Any] [d/l] |
Re: Adding 2 + 2
by jdporter (Chancellor) on Jan 30, 2007 at 19:27 UTC
|
print map { /....(.)/ and $++$+ } "@{[0..9]}";
my @c = ( my @a = each %ENV, my @b = each %SIG );
print @a . " + "
. @b . " = "
. @c . "\n";
A word spoken in Mind will reach its own level, in the objective world, by its own weight
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by Anonymous Monk on Jul 24, 2005 at 05:15 UTC
|
I don't know about 2+2, but I think 1+1=2 has been proven. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Adding 2 + 2
by ciderpunx (Vicar) on Mar 04, 2007 at 14:29 UTC
|
print 2 << 1;
| [reply] [Watch: Dir/Any] [d/l] |
Re: Adding 2 + 2
by webfiend (Vicar) on Feb 19, 2007 at 23:42 UTC
|
#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;
use HTML::Parser;
# 'print' URL to save a smidge of bandwidth
my $url = "http://perlmonks.org/?displaytype=print;node_id=31832";
if (my $page = get($url)) {
my $parser = HTML::Parser->new( api_version => 3 );
$parser->handler( start => sub {
return if shift ne 'title';
my $self = shift;
$self->handler( text => sub {
my $title = shift;
# Only ready for 4-function math with integers
if ($title =~ m#((\d+) (\+) (\d+))#) {
print "$1 = ", eval("$2 $3 $4"), "\n";
}
}, 'dtext'
);
$self->handler( end => sub {
shift->eof if shift eq 'title' ;
}, 'tagname,self'
);
},
'tagname,self'
);
$parser->parse($page);
}
else {
print "Unable to fetch '$url' for some reason.\n";
}
I chose to leave the database layer out of it this time. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2
by shmem (Chancellor) on Jan 30, 2007 at 16:49 UTC
|
print "the answer is @{[2+2]}\n";
Works under strict and warnings.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
print "the answer is ${\(2+2)}\n";
A word spoken in Mind will reach its own level, in the objective world, by its own weight
| [reply] [Watch: Dir/Any] [d/l] |
|
The scalar version is only one character longer
golfer habits...
--shmem
But I might be parling heck.
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [Watch: Dir/Any] |
Re: Adding 2 + 2
by tobyink (Canon) on May 24, 2012 at 08:15 UTC
|
use Modern::Perl;
use List::Util qw(first);
use LWP::Simple qw(get);
say for
first {/^2\+2/}
split m{\r?\n|\r},
get q{http://gutenberg.net.au/ebooks01/0100021.txt}
(And monks outside of the Australian branch of the monastery need to be aware of potential copyright issues.)
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
| [reply] [Watch: Dir/Any] [d/l] |