home *** CD-ROM | disk | FTP | other *** search
- ; examples.l - miscellaneous examples
- ;
- ;
- ; rfib - recursive fibonacci function.
- ;
- (def rfib (lambda (n)
- (cond ((< n 2) 1)
- (t (+ (rfib (- n 1)) (rfib (- n 2)))))))
-
- ; tfib - one implementation of fibonacci function.
- ; don't use EQ here, or the computation will never terminate.
- ;
- (defun tfib (x)
- (cond ( (eql x 1) 1)
- ((eql x 2) 1)
- (t (+ (tfib (- x 1))
- (tfib (- x 2))))))
- ;
- ; ifib - iterative fibonacci function.
- ;
- ; not done yet - would anybody like to contribute one ?
-
- ;
- ; ipower - iteratively coded n ^ mth power routine
- ; m and m must both be integers.
- (def ipower (lambda (m n)
- (prog ((result 1) (exponent n))
- ;
- ; first, some checking of the parameters:
- ;
- (cond
- ((not (integerp m))
- (return (err "***> IPOWER: 1st arg must be an integer")))
- ((not (integerp n))
- (return (err "***> IPOWER: 2nd arg must be an integer"))))
- ;
- ; main loop:
- ;
- ploop (cond ((zerop exponent) (return result))) ;test
- (setq result (* m result)) ;reset
- (setq exponent (- exponent 1)) ;reset
- (go ploop)))) ;repeat
- ;
- ;
- ; do_ipower - same as ipower, above,
- ; except coded with a DO instead of a PROG.
- ;
- (def do_ipower (lambda (m n)
- (do
- ((result 1) (exponent n)) ;INIT part of DO
- ((zerop exponent) result) ;TEST part of DO
- ;
- ; rest of the function is the BODY of the DO:
- ;
- (setq result (* m result))
- (setq exponent (- exponent 1)))))
- ;
- ;
- ; ifact - iteratively coded factorial
- ;
- (def ifact (lambda (e)
- (prog (result count)
- (setq result 1) ;initialize result
- (setq count e) ;initialize e
-
- ploop
- (cond
- ((<= count 0) (return result))) ;exit test
- (setq result (* result count)) ;accumulate result
- (setq count (- count 1)) ;reset
- (go ploop)))) ;repeat
-
- ;
- ; rfact - recursively coded factorial function
- ;
- (def rfact (lambda (x)
- (cond
- ((zerop x) 1)
- (t (* x (rfact (- x 1)))))))
- ;
- ; rev & app - slow versions of reverse and append which appeared
- ; in BYTE, july 1984, p 288.
- ;
- (def rev (lambda (x)
- (cond
- ((null x) nil)
- (t (app (rev (cdr x)) (list (car x)))) ) ))
- ;
- (def app (lambda (x y)
- (cond
- ((null x) y)
- (t (app (rev (cdr (rev x))) (cons (car (rev x)) y))))))
- ;
- ;
- ; end of file