|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Ackermann function
Hi,
Do you konw how should Ackermann function look in bash or sh, or any diffrent shell laguage ?? I know this function i c++ but I don't know enough shell language Thanks for any help |
| Sponsored Links | |
|
|
|
#3
|
|||
|
|||
|
I am assuming you are doing this to take a recursive algorithm that you know and learn more about shell scripting by rewriting in bash or sh? Certainly you are not doing it for performance ;-} There is a group of Ackermann functions written in many different languages here: Ackermann's Function The closest to shell form would be the Perl version. That looks like this: Code:
#!/usr/bin/perl
use strict;
use integer;
sub Ack {
my($M, $N) = @_;
return( $N + 1 ) if ($M == 0);
return( Ack($M - 1, 1) ) if ($N == 0);
Ack($M - 1, Ack($M, $N - 1));
}
my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);
my $ack = Ack(3, $NUM);
print "Ack(3,$NUM): $ack\n\n";Translating that to BASH is straightforward: Code:
#!/bin/bash
#Uncomment the 'set' if you want a line-by-line result...
#set -x
ack ()
{
local m=$1
local n=$2
if [ $m -eq 0 ]
then
return $(( $n+1 ))
fi
if [ $n -eq 0 ]
then
ack $(( $m-1 )) 1
return $?
fi
ack $m $(( $n-1 ))
ack $(( $m-1 )) $?
}
loops=0
p=$1
o=3
(( num = p<1?1:$p ))
ack $o $num
printf "ack %s %s = %s \n\n" $o $num $?There is a problem, however, with the BASH version (or any of sh, bash, etc). The return value from a subroutine or a script is 8 bits. So the ack function can only be used on really small values. It is the same as the Perl up to an input value of 4, but then produces '254' which is the largest direct return value from a subroutine. To fix that, I will leave that as an exercise to the reader... Last edited by drewk; 03-08-2010 at 11:42 AM.. Reason: typos... |
|
#4
|
|||
|
|||
|
Ok. As an exercise, I have: Code:
#!/bin/bash
function ack
{
local m=$1 n=$2
if ((m == 0)); then let r=n+1
elif ((n == 0)); then ack $((m-1)) 1
else
ack $m $((n-1))
ack $((m-1)) $r
fi
}
ack $1 $2
echo "ack($1, $2) =" $r |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Great answer!
Last edited by drewk; 03-10-2010 at 10:27 AM.. Reason: edited out wrong info... |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Function pointer to inline function ? | emitrax | Programming | 11 | 10-01-2009 03:59 AM |
| Return a value from called function to the calling function | mvictorvijayan | Shell Programming and Scripting | 1 | 09-14-2009 04:19 AM |
| Passing global variable to a function which is called by another function | sars | Shell Programming and Scripting | 4 | 06-30-2008 11:39 AM |
| Function within function (Recurance) | chassis | UNIX for Dummies Questions & Answers | 2 | 09-19-2006 09:32 AM |
| How to convert the "select" function into a "poll" function | rbolante | Programming | 1 | 07-10-2001 10:49 AM |
|
|