Some functions from McCarthy paper in Emacs Lisp

Some functions from McCarthy paper in Emacs Lisp
(defun sgn (x)
  "Signal of x."
  (cond ((< x 0)
         -1)
        (t
         1)))

(defun absx (x)
  "Absolute value of x."
  (cond ((< x 0)
         -x)
        (t
         x)))

(defun delta (i j)
  "Kronecker's delta."
  (cond ((= i j)
         1)
        (t
         0)))

(defun factorial (n)
  "Factorial function."
  (cond ((= n 1)
         1)
        (t
         (* n (factorial (- n 1))))))

(defun gcd (m n)
  "Euclidean algorithm for the greatest common divisor."
  (cond ((> m n)
         (gcd n m))
        ((= 0 (rem* n m))
         m)
        (t
         (gcd (rem* n m) m))))

(defun sqrt (a x tol)
  "Heron method for sqrt(a)."
  (cond ((< (abs (- (* x x) a)) tol)
         x)
        (t
         (sqrt a (* .5 (+ x (/ a x))) tol))))

(defun and. (p q)
  "Logical and."
  (cond (p q)
        (t nil)))

(defun or. (p q)
  "Logical or."
  (cond (p t)
        (t q)))

(defun neg (p)
  "Logical negation."
  (cond (p nil)
        (t t)))

(defun implies (p q)
  (cond (p q)
        (t t)))
Palavras chave/keywords: Lisp, McCarthy, Emacs Lisp

Última actualização/Last updated: 2014-02-20 [14:38]


1999-2014 (ç) Tiago Charters de Azevedo

São permitidas cópias textuais parciais/integrais em qualquer meio com/sem alterações desde que se mantenha este aviso.

Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.