What is wrong with this recursive Windows CMD script? It won't do Ackermann properly

Posted by boost on Stack Overflow See other posts from Stack Overflow or by boost
Published on 2010-04-21T06:01:08Z Indexed on 2010/04/21 6:03 UTC
Read the original article Hit count: 257

I've got this code that I'm trying to get to calculate the Ackermann function so that I can post it up on RosettaCode. It almost works. I thought maybe there'd be a few batch file wizards on StackOverflow.

::echo off
set depth=0
:ack
if %1==0 goto m0
if %2==0 goto n0

:else
set /a n=%2-1
set /a depth+=1
call :ack %1 %n%
set t=%errorlevel%
set /a depth-=1
set /a m=%1-1
set /a depth+=1
call :ack %m% %t%
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

:m0
set/a n=%2+1
if %depth%==0 ( exit %n% ) else ( exit /b %n% )

:n0
set /a m=%1-1
set /a depth+=1
call :ack %m% %2
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

I use this script to test it

@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann of %1 %2 is %errorlevel%

A sample output, for Test 1 1, gives:

>test 1 1
>set depth=0
>if 1 == 0 goto m0
>if 1 == 0 goto n0
>set /a n=1-1
>set /a depth+=1
>call :ack 1 0
>if 1 == 0 goto m0
>if 0 == 0 goto n0
>set /a m=1-1
>set /a depth+=1
>call :ack 0 0
>if 0 == 0 goto m0
>set/a n=0+1
>if 2 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>if 1 == 0 (exit 1  )  else (exit /b 1  )
>set t=1
>set /a depth-=1
>set /a m=1-1
>set /a depth+=1
>call :ack 0 1
>if 0 == 0 goto m0
>set/a n=1+1
>if 1 == 0 (exit 2  )  else (exit /b 2  )
>set t=2
>set /a depth-=1
>if 0 == 0 (exit 2  )  else (exit /b 2  )
Ackermann of 1 1 is 2

© Stack Overflow or respective owner

Related posts about xp-cmdshell

Related posts about cmd.exe