Factorial (1) : Michael Neumann

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Perl

http://www.perl.com

Factorial (1) Michael Neumann


sub fac { my ($n) = @_; if ($n < 2) { return $n; } else { return $n * fac($n-1); } !"n!;

print fac(6)

Calculates the factorial. Results #2$.

Factorial (2) Arnaud A A!


%&'usr'bin'perl sub fac { $_($)*1+$_($)*fac($_($)-1),1; } print fac(6);

Calculates the factorial. Results #2$.

"ello #orld Michael Neumann


% -ell. /.rl0 in 1erl print "Hello World\n";

$rints %"ello #orld% onto the screen.

References Michael Neumann

% array -* reference @arr = (1 2 2); $arr_ref = "@arr; % reference -* array $arr_ref2 = (1 2 2); @arr2 = @$arr_ref2; % array access $item = $arr($); $item = $arr_ref-*($);

References and Arra&s' Arra& access

(uares (1) Michael Neumann


for($i = 1; $i <= 1$; 33$i) { print $i*$i ' '; }

)utputs the s(uares from 1 to 1*.

(uares (2) Michael Neumann


@s4uares = (); f.reac5 my $i (1661$) { pus5(@s4uares $i * $i); } print 7.in(! ! @s4uares) !"n!;

)utputs the s(uares from 1 to 1*.

(uares (+) $i,el


print 7.in(! ! map { $_ ** 2 } 1661$) !"n!;

)utputs the s(uares from 1 to 1*.

(uares (-) Arnaud A A!


f.r (1661$) { print $_**2 ! !};print$';

)utputs the s(uares from 1 to 1*.

u.stitution Michael Neumann


$a = 2; $b = 8; $c = $a 3 $b; print "$a + $b = $c";

$rints 2 3 8 = 9 onto the screen.

/cl''''''''''''''''''''''

Tcl

"ello #orld Michael Neumann


puts !-ell. /.rl0!

$rints %"ello #orld% onto the screen.

(uares Michael Neumann


set i 1 :5ile {$i <= 1$} {puts (e;pr $i*$i);set i (e;pr $i31)}

)utputs the s(uares from 1 to 1*.

u.stitution Michael Neumann


set a 2 set b 8

puts !$a 3 $b = (e;pr $a 3 $b)!

$rints 2 3 8 = 9 onto the screen.

"ello #orld Tcl/Tk Michael Neumann


butt.n 65ell. -te;t !-ell. /.rl0! -c.mman0 {0estr.y 6} pac< 65ell.

$rints %"ello #orld% onto the screen.

You might also like