From: Sungwoo, Lim
Subject: [Q] multiple comparison?
Date: 
Message-ID: <150320011552265287%sungwoo@cad.strath.ac.uk>
Hello,

Is there any way to reduce the length of the below code?
Each a1 ~ a5 has a respective value, so it cannot be explained as 
(>= a1 a2 a3 a4 a5 0.5).

;-------------
(and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))

Thanks for help. =)

Sungwoo

From: Geoff Summerhayes
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <tb1sek6i48i92d@corp.supernews.com>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> wrote in message
·······························@cad.strath.ac.uk...
> Hello,
>
> Is there any way to reduce the length of the below code?
> Each a1 ~ a5 has a respective value, so it cannot be explained as
> (>= a1 a2 a3 a4 a5 0.5).
>
> ;-------------
> (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
>

Why? It looks very efficient in comparison to these:

(>= (min a1 a2 a3 a4 a5) 0.5)

(= (min a1 a2 a3 a4 a5 0.5) 0.5)

These are better, but I think the original is still the best:

(every #'(lambda(x)(>= x 0.5)) (a1 a2 a3 a4 a5))

(notany #'(lambda(x)(< x 0.5)) (a1 a2 a3 a4 a5))


Geoff
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011716098535%sungwoo@cad.strath.ac.uk>
In article <··············@corp.supernews.com>, Geoff Summerhayes
<·············@hNoOtSmPaAiMl.com> wrote:

> "Sungwoo, Lim" <·······@cad.strath.ac.uk> wrote in message
> ·······························@cad.strath.ac.uk...
> > Hello,
> >
> > Is there any way to reduce the length of the below code?
> > Each a1 ~ a5 has a respective value, so it cannot be explained as
> > (>= a1 a2 a3 a4 a5 0.5).
> >
> > ;-------------
> > (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> >
> 
> Why? It looks very efficient in comparison to these:
> 
> (>= (min a1 a2 a3 a4 a5) 0.5)
> 
> (= (min a1 a2 a3 a4 a5 0.5) 0.5)
> 
> These are better, but I think the original is still the best:
> 
> (every #'(lambda(x)(>= x 0.5)) (a1 a2 a3 a4 a5))
> 
> (notany #'(lambda(x)(< x 0.5)) (a1 a2 a3 a4 a5))
> 
> 
> Geoff

Hehe,, yeah...
The original code looks much simpler.
Maybe this is correct?

> (every #'(lambda(x)(>= x 0.5)) (a1 a2 a3 a4 a5))
  (every #'(lambda(x)(>= x 0.5)) (list a1 a2 a3 a4 a5))

Thanks alot, =)

Sungwoo
From: Geoff Summerhayes
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <tb22f1nkg9qc39@corp.supernews.com>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> wrote in message
·······························@cad.strath.ac.uk...
> In article <··············@corp.supernews.com>, Geoff Summerhayes
> <·············@hNoOtSmPaAiMl.com> wrote:
>
> Hehe,, yeah...
> The original code looks much simpler.
> Maybe this is correct?
>
> > (every #'(lambda(x)(>= x 0.5)) (a1 a2 a3 a4 a5))
>   (every #'(lambda(x)(>= x 0.5)) (list a1 a2 a3 a4 a5))

Damn.

NOTE TO SELF: Remember to use cut-and-paste from the listener
                    instead of trying to re-type it!

:-)
Geoff
From: Frode Vatvedt Fjeld
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <2hn1anc8zf.fsf@dslab7.cs.uit.no>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> Is there any way to reduce the length of the below code?
> [..]
> (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))

You could say the following, which does scale better, although I
wouldn't call it an improvement in the constant case n=5:

  (every #'(lambda (a) (>= a 0.5))
         (list a1 a2 a3 a4 a5))

or even this, somewhat uglier:

  (every #'>=
         (list a1 a2 a3 a4 a5)
         (make-list 5 :initial-element 0.5))

-- 
Frode Vatvedt Fjeld
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011707186595%sungwoo@cad.strath.ac.uk>
In article <··············@dslab7.cs.uit.no>, Frode Vatvedt Fjeld
<······@acm.org> wrote:

> "Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:
> 
> > Is there any way to reduce the length of the below code?
> > [..]
> > (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> 
> You could say the following, which does scale better, although I
> wouldn't call it an improvement in the constant case n=5:
> 
>   (every #'(lambda (a) (>= a 0.5))
>          (list a1 a2 a3 a4 a5))
> 
> or even this, somewhat uglier:
> 
>   (every #'>=
>          (list a1 a2 a3 a4 a5)
>          (make-list 5 :initial-element 0.5))

Oh,, I see.
Thanks alot. 

Sungwoo
From: Kent M Pitman
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <sfwbsr33q3p.fsf@world.std.com>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> In article <··············@dslab7.cs.uit.no>, Frode Vatvedt Fjeld
> <······@acm.org> wrote:
> 
> > "Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:
> > 
> > > Is there any way to reduce the length of the below code?
> > > [..]
> > > (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> > 
> > You could say the following, which does scale better, although I
> > wouldn't call it an improvement in the constant case n=5:
> > 
> >   (every #'(lambda (a) (>= a 0.5))
> >          (list a1 a2 a3 a4 a5))
> > 
> > or even this, somewhat uglier:
> > 
> >   (every #'>=
> >          (list a1 a2 a3 a4 a5)
> >          (make-list 5 :initial-element 0.5))

It's a pity we didn't put make-circular-list into CL.  It's better
for this case (and easy to write...)  Though for a constant, just
'#1=(0.5 . #1#) will suffice, I guess.  And the constant saves back
some of the superfluous consing, here.
 
> Oh,, I see.
> Thanks alot. 

I have a question still--why are you trying to optimize the length
rather than, for example, the speed.  The total code size here is tiny
and surely the speed gain from doing a the original form is well worth
it.  I'm not sure I understand the problem being solved.
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011805267310%sungwoo@cad.strath.ac.uk>
In article <···············@world.std.com>, Kent M Pitman
<······@world.std.com> wrote:
 
> It's a pity we didn't put make-circular-list into CL.  It's better
> for this case (and easy to write...)  Though for a constant, just
> '#1=(0.5 . #1#) will suffice, I guess.  And the constant saves back
> some of the superfluous consing, here.

Those variables (a1, a2, a3, a4, a5) are keep changed stroke by stroke,
so it is not possible to replace it as a concrete constant... 
I don't know how '#1=(0.5 . #1#) works....
Would you explain more about this?

> I have a question still--why are you trying to optimize the length
> rather than, for example, the speed.  The total code size here is tiny
> and surely the speed gain from doing a the original form is well worth
> it.  I'm not sure I understand the problem being solved.

Well, because I am not a expert of CL, I wanted to see other people's
alternatives. So I can learn more programming skills.
Of course, I prefer speed rather than length. =)

Thanks, =)

Sungwoo
From: Kent M Pitman
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <sfw8zm69913.fsf@world.std.com>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> In article <···············@world.std.com>, Kent M Pitman
> <······@world.std.com> wrote:
>  
> > It's a pity we didn't put make-circular-list into CL.  It's better
> > for this case (and easy to write...)  Though for a constant, just
> > '#1=(0.5 . #1#) will suffice, I guess.  And the constant saves back
> > some of the superfluous consing, here.
> 
> Those variables (a1, a2, a3, a4, a5) are keep changed stroke by stroke,
> so it is not possible to replace it as a concrete constant... 
> I don't know how '#1=(0.5 . #1#) works....
> Would you explain more about this?

The reader (i.e., the READ function) allows you to notate sharing by
#n= (to define something you want to repeat later) and #n# (to recall it).
So that thing above is a constant which is the same circular list you'd get
by doing:

 '#.(let ((cons (cons 0.5 nil))) ;#1=(0.5 . nil)
      (setf (cdr cons) cons)     ;#1=(0.5 . #1#)
      cons)

If you're not familiar with #., it's just something that reads a subsequent
form, evaluates it, and inserts the result of that evaluation into the same
point in the read.  e.g., '(foo #.(+ 1 1)) is the same as '(foo 2).

> > I have a question still--why are you trying to optimize the length
> > rather than, for example, the speed.  The total code size here is tiny
> > and surely the speed gain from doing a the original form is well worth
> > it.  I'm not sure I understand the problem being solved.
> 
> Well, because I am not a expert of CL, I wanted to see other people's
> alternatives. So I can learn more programming skills.
> Of course, I prefer speed rather than length. =)

In general, if you have a small number of operations which you have good
reason to believe the compiler will optimize into instructions rather than
function calls (such as eq, eql, =, >, <, etc.), and if you think the 
comparisons will be done a lot, it's sometimes worth the speed gain just
to do the AND and an explicit set of compares.  If the code is either called
very seldom or else uses functions that probably the compiler won't optimize,
then the cost of using other operators like EVERY diminishes as a proportion
of the total cost you're incurring, and it becomes purely a matter of what
you feel expresses your intent in the most clear fashion.  Or such is my
personal opinion.

> Thanks, =)

Sure.
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011845533873%sungwoo@cad.strath.ac.uk>
In article <···············@world.std.com>, Kent M Pitman
<······@world.std.com> wrote:

> The reader (i.e., the READ function) allows you to notate sharing by
> #n= (to define something you want to repeat later) and #n# (to recall it).
> So that thing above is a constant which is the same circular list you'd get
> by doing:
> 
>  '#.(let ((cons (cons 0.5 nil))) ;#1=(0.5 . nil)
>       (setf (cdr cons) cons)     ;#1=(0.5 . #1#)
>       cons)
> 
> If you're not familiar with #., it's just something that reads a subsequent
> form, evaluates it, and inserts the result of that evaluation into the same
> point in the read.  e.g., '(foo #.(+ 1 1)) is the same as '(foo 2).

> In general, if you have a small number of operations which you have good
> reason to believe the compiler will optimize into instructions rather than
> function calls (such as eq, eql, =, >, <, etc.), and if you think the 
> comparisons will be done a lot, it's sometimes worth the speed gain just
> to do the AND and an explicit set of compares.  If the code is either called
> very seldom or else uses functions that probably the compiler won't optimize,
> then the cost of using other operators like EVERY diminishes as a proportion
> of the total cost you're incurring, and it becomes purely a matter of what
> you feel expresses your intent in the most clear fashion.  Or such is my
> personal opinion.
> 

I see. Thanks again. =)
Sungwoo
From: Christopher J. Vogt
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <3AB109D9.9EFF4128@computer.org>
"Sungwoo, Lim" wrote:
> 
> Hello,
> 
> Is there any way to reduce the length of the below code?
> Each a1 ~ a5 has a respective value, so it cannot be explained as
> (>= a1 a2 a3 a4 a5 0.5).
> 
> ;-------------
> (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> 

maybe?: (>= (min a1 a2 a3 a4 a5) 0.5)
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011848232902%sungwoo@cad.strath.ac.uk>
In article <·················@computer.org>, Christopher J. Vogt
<····@computer.org> wrote:

> "Sungwoo, Lim" wrote:
> > 
> > Hello,
> > 
> > Is there any way to reduce the length of the below code?
> > Each a1 ~ a5 has a respective value, so it cannot be explained as
> > (>= a1 a2 a3 a4 a5 0.5).
> > 
> > ;-------------
> > (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> > 
> 
> maybe?: (>= (min a1 a2 a3 a4 a5) 0.5)

Yeap, thanks. =)

Sungwoo
From: Vladimir V. Zolotych
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <3AB1040C.AA19C168@eurocom.od.ua>
"Sungwoo, Lim" wrote:
> 
> (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))

Variables are evaluated in order
(loop for v in '(a1 a2 a3 a4 a5) always (>= (symbol-value v) 0.5)))
if a1 a2.. are special.

-- 
Vladimir Zolotych                         ······@eurocom.od.ua
From: Sungwoo, Lim
Subject: Re: [Q] multiple comparison?
Date: 
Message-ID: <150320011816066002%sungwoo@cad.strath.ac.uk>
In article <·················@eurocom.od.ua>, Vladimir V. Zolotych
<······@eurocom.od.ua> wrote:

> "Sungwoo, Lim" wrote:
> > 
> > (and (>= a1 0.5) (>= a2 0.5) (>= a3 0.5) (>= a4 0.5) (>= a5 0.5))
> 
> Variables are evaluated in order
> (loop for v in '(a1 a2 a3 a4 a5) always (>= (symbol-value v) 0.5)))
> if a1 a2.. are special.

Thanks, =)

Sungwoo