Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2010
Registered User
 

Join Date: Feb 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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
    #2  
Old 03-07-2010
pludi's Avatar
pludi pludi is offline Forum Staff  
Cat herder
 

Join Date: Dec 2008
Location: Vienna, Austria, Earth
Posts: 5,486
Thanks: 38
Thanked 324 Times in 301 Posts
Reference for those who don't know what the Ackermann function is:link
Sponsored Links
    #3  
Old 03-07-2010
Registered User
 

Join Date: Mar 2010
Location: la jolla, ca
Posts: 100
Thanks: 4
Thanked 3 Times in 3 Posts
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  
Old 03-09-2010
Registered User
 

Join Date: Aug 2009
Location: New Jersey
Posts: 313
Thanks: 6
Thanked 66 Times in 54 Posts
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  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Location: la jolla, ca
Posts: 100
Thanks: 4
Thanked 3 Times in 3 Posts
Great answer!

Last edited by drewk; 03-10-2010 at 10:27 AM.. Reason: edited out wrong info...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 12:53 AM.