[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Axiom-developer] The lisp function ONEP
From: |
Jaap Weel |
Subject: |
[Axiom-developer] The lisp function ONEP |
Date: |
Fri, 26 Sep 2003 16:50:55 -0700 |
ONEP is a common predicate in some older Lisps, but not in Common Lisp.
It appears in McCarthy's Lisp 1.5
(http://green.iis.nsk.su/~vp/doc/lisp1.5/mccarthy.html). It's supposed
to test if it's argument is equal to 1. Problem is that in the Lisp
world, there are various interpretations of "equal" and "1". Let the
show begin.
McCarthy in the mentioned reference writes:
onep[x] is true if |x-1| <= 3*10^6
So far so good.
From simp.lisp in the Maxima sources, which probably reflects Maclisp:
";; The following definitions of ONEP and ONEP1 are bummed for speed,
and should
;; be moved to a special place for implementation dependent code.
;; ONEP is the same as (EQUAL A 1), but does the check inline rather
than
;; calling EQUAL (uses more instructions, so this isn't done by
default). ONEP
;; seems to be used very rarely, so it seems hardly worth the effort.
On the
;; Lisp Machine, this is probably more efficient as simply (EQUAL A 1).
#+(and cl (not cmu))
(defmacro onep (a) `(eql ,a 1))
#+cl
(DEFMFUN ONEP1 (A) (OR (and (numberp a) (= A 1)) (EQUAL A BIGFLOATONE)))
#-cl
(progn 'compile
(DEFMFUN ONEP (A)
#-NIL (AND (EQ (ml-typep A) 'fixnum) (= A 1))
#+NIL (eql a 1))
#-(or Franz cl)
(DEFMFUN ONEP1 (A) (OR (EQUAL A 1) (EQUAL A 1.0) (EQUAL A
BIGFLOATONE)))"
However, in the Standard Lisp Report (an early standardization effort):
"
ONEP(U:any):boolean eval, spread.
Returns T if U is a number and has the value 1 or 1.0. Returns NIL
otherwise.
EXPR PROCEDURE ONEP(U);
OR(EQN(U, 1), EQN(U, 1.0));
[The definition in the published report is incorrect as it does not
return T for U of 1.0.]
"
There's clearly a lot of confusion about what ONEP should do. I think,
however, that you can easily check experimentally in a GCL interpreter
which interpretation GCL uses. I don't have a GCL here, but it
shouldn't be hard. Keep in mind the difference between the functions =,
eq, eql, and equal. For that, see sections 6.3 and 12.3 in CLtL2
(Common Lisp the Language 2, available online).
With SBCL:
* (= 1 1.0)
T
* (eq 1 1.0)
NIL
* (eql 1 1.0)
NIL
* (equal 1 1.0)
NIL
* (= 1 1)
T
* (eq 1 1)
T
* (eql 1 1)
T
* (equal 1 1)
T
At any rate, it seems likely that either of the following is the right
thing:
(defun onep (x) (= x 1))
(defun onep (x) (eq x 1))
I don't think McCarthy's definition is relevant.
========================================================================
Jaap Weel Campus address: | dorm (626) 795-9748
Caltech, Blacker '05 Caltech MSC #874, Pasadena, CA 91126, U.S.A.
www.its.caltech.edu/~weel Permanent address: | home +31-46-4337033
E-mail: address@hidden Kelderstraat 2-4, 6171 GB Stein, Netherlands
========================================================================
- [Axiom-developer] The lisp function ONEP,
Jaap Weel <=