if iw ant to check if an integer is odd or even there is a builtin yes
but if i want to define one.
in most languages i can do if x%2 == x/2 then ...else...
how do i do this in lisp?
On May 15, 3:05 pm, globalrev <·········@yahoo.se> wrote:
> if iw ant to check if an integer is odd or even there is a builtin yes
> but if i want to define one.
> in most languages i can do if x%2 == x/2 then ...else...
>
> how do i do this in lisp?
Actually, I think you'd rather do something like:
# Python
def is_even(n):
return 0 == (n % 2)
In Lisp, you can use "mod" and "zerop."
Tayssir
On May 15, 3:22 pm, Tayssir John Gabbour <············@googlemail.com>
wrote:
> On May 15, 3:05 pm, globalrev <·········@yahoo.se> wrote:
> > if iw ant to check if an integer is odd or even there is a builtin yes
> > but if i want to define one.
>
> Actually, I think you'd rather do something like:
(But lest anyone be confused by my response, note that the original
poster explicitly asked for an alternative to the built-in functions
"oddp" and "evenp"..)
P� Thu, 15 May 2008 15:05:43 +0200, skrev globalrev <·········@yahoo.se>:
> if iw ant to check if an integer is odd or even there is a builtin yes
> but if i want to define one.
> in most languages i can do if x%2 == x/2 then ...else...
>
> how do i do this in lisp?
oddp
--------------
John Thingstad
globalrev <·········@yahoo.se> writes:
> if iw ant to check if an integer is odd or even there is a builtin yes
> but if i want to define one.
> in most languages i can do if x%2 == x/2 then ...else...
You can do exactly that in lisp, if you insist on writing obfuscated
code:
CL-USER 42 > (defparameter *x* 2)
*X*
CL-USER 43 > (defparameter *y* 13)
*Y*
CL-USER 44 > (if (= (floor *x* 2) (/ *x* 2)) :even :odd)
:EVEN
CL-USER 45 > (if (= (floor *y* 2) (/ *y* 2)) :even :odd)
:ODD
CL-USER 46 >
--
(espen)
globalrev <·········@yahoo.se> writes:
> if iw ant to check if an integer is odd or even there is a builtin yes
> but if i want to define one.
> in most languages i can do if x%2 == x/2 then ...else...
Well, in most languages you would get wrong results...
> how do i do this in lisp?
(defun even1 (x) (zerop (ldb (byte 1 0) x)))
(defun even2 (x) (zerop (mod x 2)))
(defun even3 (x) (zerop (rem x 2))) ; works too
(defun even4 (x) (= (truncate x 2) (/ x 2))) ; is perhaps what you had in mind, but it's awful.
(defun even5 (x) (= 1 (denominator (/ x 2)))) ; even worse
(defun even6 (x) (plusp (expt -1 x))) ; we can have fun...
--
__Pascal Bourguignon__
···@informatimago.com (Pascal J. Bourguignon) writes:
> globalrev <·········@yahoo.se> writes:
>
> > if iw ant to check if an integer is odd or even there is a builtin yes
> > but if i want to define one.
> > in most languages i can do if x%2 == x/2 then ...else...
>
> Well, in most languages you would get wrong results...
>
> > how do i do this in lisp?
>
> (defun even1 (x) (zerop (ldb (byte 1 0) x)))
> (defun even2 (x) (zerop (mod x 2)))
> (defun even3 (x) (zerop (rem x 2))) ; works too
> (defun even4 (x) (= (truncate x 2) (/ x 2))) ; is perhaps what you had in mind, but it's awful.
> (defun even5 (x) (= 1 (denominator (/ x 2)))) ; even worse
> (defun even6 (x) (plusp (expt -1 x))) ; we can have fun...
(defun even7 (x) (integerp (/ x 2)))
; or the uglier: (typep (/ x 2) 'integer)
(defun even8 (x) (multiple-value-bind (q r) (truncate x 2)
(declare (ignore q))
(= r 0)))
--
Thomas A. Russ, USC/Information Sciences Institute
globalrev <·········@yahoo.se> wrote:
> if iw ant to check if an integer is odd or even there is a builtin yes
> but if i want to define one.
> in most languages i can do if x%2 == x/2 then ...else...
You're strange. x%2 == x/2 iff x in {0, +/-3}. This doesn't seem a good
way to decide whether x is odd or even.
(-3 only occurs if you're using truncate or ceiling division; most
languages which use % to mean remainder seem to, Python being the
obvious exception. +3 only occurs if you're using truncate or floor
division.)
The EVENP and ODDP functions determine evenness/oddness. To do the
strange thing you actually asked for,
(multiple-value-call #'= (<func> x 2))
where <func> is one of TRUNCATE, FLOOR, CEILING, or ROUND.
(Ooh, an actual use for MULTIPLE-VALUE-CALL!)
-- [mdw]
Mark Wooding <···@distorted.org.uk> writes:
> You're strange. x%2 == x/2 iff x in {0, +/-3}. This doesn't seem a good
> way to decide whether x is odd or even.
Ouch, I didn't even notice he used the remainder operator and not
integer division when I answered him. I guess my DWIM-module has
gotten very sloppy for non-lisp languages over the last few years :-)
--
(espen)