home *** CD-ROM | disk | FTP | other *** search
-
- (user expert)
- (message "\n\n\nWelcome to some recursive math...\n\n\n"
- "Please have a look at the source to see how to\n"
- "program recursive functions and how to use\n"
- "local environments of procedures!")
-
- ; --------------------------------------------------------------------------------
- ; define a recursive procedure which calculates the faculty of
- ; a given argument. use the LET function to create a local environment
- ; note: for arguments greater than 12 there will be an overflow,
- ; but this does not result in any runtime error
-
- (procedure faculty fac
-
- (let (set f fac)
-
- (if (= f 1)
- 1
- (* f (faculty (- f 1)))
- )
- )
- )
-
- ; --------------------------------------------------------------------------------
- ; the function "Ackerman" is a extremly recursive function, which only
- ; runs with local variables. thus here you must use the LET function
- ;
- ; the definition of the ackerman function is:
- ; a(0,y) = y+1
- ; a(x,0) = a(x-1,1)
- ; a(x,y) = a(x-1,a(x,y-1))
-
- (procedure ackerman a b
-
- (let (set x a y b)
-
- (if (= x 0)
- (+ y 1)
- (if (= y 0)
- (ackerman (- x 1) 1)
- (ackerman (- x 1) (ackerman x (- y 1)))
- )
- )
- )
- )
-
- ; --------------------------------------------------------------------------------
- ; ask for a number and then calculate the fac of this number
-
- (set fac (asknumber (prompt "Faculty of what ?")
- (help "enter the number you want to know the faculty of")
- (default 3)
- (range 1 12)
- )
- )
-
- ; --------------------------------------------------------------------------------
- ; calculate and print the faculty
-
- (message "Faculty of " fac " = " (faculty fac)
- "\n\n"
- "The result of Ackerman(1,2) = " (ackerman 1 2)
- )
-
- ; --------------------------------------------------------------------------------
- ; avoid stupid welcome... :)
-
- (exit (quiet))
- (welcome)
-
-