From: globalrev
Subject: division with rest?
Date: 
Message-ID: <df6e02cb-68b4-455e-b0b8-7c2f04065118@x35g2000hsb.googlegroups.com>
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?

From: Tayssir John Gabbour
Subject: Re: division with rest?
Date: 
Message-ID: <c3b9dc1c-ecd6-4d14-9921-02a3183341d8@34g2000hsh.googlegroups.com>
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
From: Tayssir John Gabbour
Subject: Re: division with rest?
Date: 
Message-ID: <8c93a8f9-4bf9-44bf-b0e7-a1876c918bc6@d1g2000hsg.googlegroups.com>
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"..)
From: John Thingstad
Subject: Re: division with rest?
Date: 
Message-ID: <op.ua65d0c8ut4oq5@pandora.alfanett.no>
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
From: Espen Vestre
Subject: Re: division with rest?
Date: 
Message-ID: <m1hccz8xq8.fsf@gazonk.netfonds.no>
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)
From: Pascal J. Bourguignon
Subject: Re: division with rest?
Date: 
Message-ID: <7ck5hvzjr4.fsf@pbourguignon.anevia.com>
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__
From: Thomas A. Russ
Subject: Re: division with rest?
Date: 
Message-ID: <ymiprrntsuw.fsf@blackcat.isi.edu>
···@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
From: Zach Beane
Subject: Re: division with rest?
Date: 
Message-ID: <m3abir5ss9.fsf@unnamed.xach.com>
···@informatimago.com (Pascal J. Bourguignon) writes:

> (defun even1 (x) (zerop (ldb (byte 1 0) x)))

(defun evenkt (x) (not (logbitp 0 x)))

Zach
From: Thomas A. Russ
Subject: Re: division with rest?
Date: 
Message-ID: <ymilk2btess.fsf@blackcat.isi.edu>
Zach Beane <····@xach.com> writes:

> ···@informatimago.com (Pascal J. Bourguignon) writes:
> 
> > (defun even1 (x) (zerop (ldb (byte 1 0) x)))
> 
> (defun evenkt (x) (not (logbitp 0 x)))
             ^^           ^^^^^^^

I think you need to rename this variant.  KT doesn't know about the
logbitp function.  

-- 
From: Ken Tilton
Subject: Re: division with rest?
Date: 
Message-ID: <482cc4d2$0$11601$607ed4bc@cv.net>
Thomas A. Russ wrote:
> Zach Beane <····@xach.com> writes:
> 
> 
>>···@informatimago.com (Pascal J. Bourguignon) writes:
>>
>>
>>>(defun even1 (x) (zerop (ldb (byte 1 0) x)))
>>
>>(defun evenkt (x) (not (logbitp 0 x)))
> 
>              ^^           ^^^^^^^
> 
> I think you need to rename this variant.  KT doesn't know about the
> logbitp function.  
> 

Finally I am getting some support around here!

kenny

-- 
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
ECLM rant: 
http://video.google.com/videoplay?docid=-1331906677993764413&hl=en
ECLM talk: 
http://video.google.com/videoplay?docid=-9173722505157942928&q=&hl=en
From: Mark Wooding
Subject: Re: division with rest?
Date: 
Message-ID: <slrng36oq5.ihm.mdw@metalzone.distorted.org.uk>
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]
From: Espen Vestre
Subject: Re: division with rest?
Date: 
Message-ID: <m1wslnx69t.fsf@vestre.net>
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)