1. questions, questions; always questions :)

this last round of optimizing simplistic functions (ABS for one)
has left me with this real nagging question...

*ponder*... how to explain this... hrmmmmmm

take Carl's code to do abs for example:
    return x * (x>0) - (x<0)

it's the part where (x>0) is actually looking at the
==whole== sequence.  but you cannot do that here:
    if (x>0) then blah end if
because at that point, x must be an atom or the resultant
expression must be an atom to be more precise...
so why can you make a comparison that involves an entire
sequence or an operation that involves an entire
sequence in a return line but cannot do the same thing
in a ==real== comparison statement???
another example is supposing again x and y are a sequence
you cannot do:
   if (x*3) > y then blah
but you can do:
   return x*3 > y

some would say that you should use compare!
you can... sometimes...
but not for Carl's example, for example--heh.
   if (compare(x,0)=1) - (compare(x,0)=-1 )
well that doesn't work for sequences cuz 0 is an atom...
but compare(x*3,y)=1 would work for the other example...

so why the disparity between return statements and
any of the conditionals as to how they relate to sequences???
seems to me that if you were to have me choose which
commands had more priority, ie: which ones were to get
more beefed up interpretations, I woulda picked having
the conditionals (if.then,while.do etc) being able to
operate on entire sequences before I woulda picked having
'return' operate as it does... seems to me the conditionals
are a "higher priority" type of situation as they are
used more and and in fact are the ones that should be
doing comparisions in the first place...
return returns values, and conditionals check conditions.
having return check conditions on that magnitude and
the conditionals not being able to do that...
but i'm not complaining :)

--Hawke'
(side note: jiri?? was it?? mentioned the counter-intuitive
nature of compare... i figured out a way to think about
compare that makes sense... if you don't think of it like:
   is x and y the same?
but instead think:
   is x lessthan, equalto, or greaterthan y?
which corresponds to compare equaling -1, 0 or 1...
helps me... and i don't wind up forgetting that damned =0
and lest we forget the good ol'fashioned IsSame() command :)

new topic     » topic index » view message » categorize

2. Re: questions, questions; always questions :)

Hawke wrote:

>*ponder*... how to explain this... hrmmmmmm
>
>take Carl's code to do abs for example:
>    return x * (x>0) - (x<0)
>
>it's the part where (x>0) is actually looking at the
>==whole== sequence.  but you cannot do that here:
>    if (x>0) then blah end if
>because at that point, x must be an atom or the resultant
>expression must be an atom to be more precise...
>so why can you make a comparison that involves an entire
>sequence or an operation that involves an entire
>sequence in a return line but cannot do the same thing
>in a ==real== comparison statement???

Hawke, the same or a very similar question already surfaced several
times on this list, and I am still not sure what you want. The problem
is not in the comparison itself, it is probably even happily performed
(unless the compiler is more intelligent than I think it is), but the
simpleton conditional does not know which way to jump when faced with
a sequence. What is your advice it should do? - I hope this is not
just another attempt to smuggle a case statement into Euphoria through
the back door smile.

My dislike of the compare() function is purely superficial: a
comparison test should logically return TRUE when the objects are the
same ('comparable') and FALSE when they are not. I suspect the actual
function's real name is IsNotSame(), and the unexpected bonus of the
doubled truth (-1 and 1) is a legacy from another language from a very
forgettable era. jiri

new topic     » goto parent     » topic index » view message » categorize

3. Re: questions, questions; always questions :)

>you cannot do:
>   if (x*3) > y then blah
>but you can do:
>   return x*3 > y

Hmm... I know why this is, so I'll try to explain it...

"if" wants to return an atom, but "return" can return objects:

object x
atom y

x = {1, 2, 3, 4}
y = 7

if (x*3) > y then blah end if
-- {1, 2, 3, 4} * 3 = {3, 6, 9, 12}
-- {3, 6, 9, 12} > y = {0, 0, 1, 1}
-- "if" doesn't know if the result is true or false, so it doesn't work.

return x*3 > y
-- {1, 2, 3, 4} * 3 = {3, 6, 9, 12}
-- {3, 6, 9, 12} > y = {0, 0, 1, 1}
-- So, it's basically saying, "return {0, 0, 1, 1}", which is legal.


ICQ UIN: 17645603


_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

4. Re: questions, questions; always questions :)

Robert also replied (thanks as well)
jiri babor wrote:
> Hawke wrote:
> >take Carl's code to do abs for example:
> >    return x * (x>0) - (x<0)
> >
> >it's the part where (x>0) is actually looking at the
> >==whole== sequence.  but you cannot do that here:
> >    if (x>0) then blah end if
> >because at that point, x must be an atom or the resultant
> >expression must be an atom to be more precise...
looking more closely at this above line is the crux of my
dilemma.  As robert also pointed out,
  --sequence x
  --x={12,34,-1,22,-118,3422}
  --if (x>0) then blah end if
(x>0) becomes {1,1,0,1,0,1}
right? this is the =intermediate= result, but not the
=final= conditional test...
the final test is taking the intermediate result and
jumping based upon the intermediate result being 1 or 0.
  --sidebar to illustrate
  -- if (3>7) then UniverseIsFried() end if
     here, we see the intermediate being FALSE, and the
     final being: if TRUE then do the if..then, otherwise
     jump to the next "elsif" or "else" or "end if"

back to the example (x>0) now:
since there are 0's in the intermediate result,
the final expression/result is FALSE.
because the expression (x>0) is FALSE it shouldn't
do the "if..then" statement...
this type of evaluation is impossible in euphoria.
you cannot "compare" using atoms and sequences and
you cannot evaluate sequences as a whole in conditional
expressions (if..then, while..do ...etc).
in robert's example:
   --if x*3 > y then blah end if   --x={1,2,3,4} y=7
you get {3,6,9,12} > 7  and that yields {0,0,1,1}
as the intermediate result.  that intermediate result, to
me, should be FALSE (has zeros).
here's one that is TRUE:
  -- x={200,432,56,618}    MaxX=640
  -- y={112,252,35,460}    MaxY=480
  -- if (x<MaxX) and (y<MaxY) and
  --    (x>=0)   and (y>=0)   then
  --    plot(x,y,GREEN)
  -- end if
  -- now look at the intermediate/final results:
  -- if {1,1,1,1} and {1,1,1,1} and
        {1,1,1,1} and {1,1,1,1} then
        plot(x,y,GREEN) end if
  -- if TRUE and TRUE and TRUE and TRUE then
  -- if TRUE then   ---okay we do the if..then statement.
no, i am not looking for 'case' here
i am looking for =consistency=.
you cannot compare an atom against an entire sequence without
using 'return' or a kludge.  that, to me, is inconsistent.
yes, the above could be rewritten, but it shouldn't
*have to be* rewritten.  my point is that if..then
and other conditionals *should* know what to do with
{0,1,1,0,1,1,0} (FALSE)  and {1,1,1,1} (TRUE).

note: i do however still believe that for seq vs seq
testing, that 'compare' or some other such thing/mechanism
be used.  atoms vs seq's  comparisons and seq vs seq
comparisons are apples and oranges apart.

> Hawke, the same or a very similar question already surfaced several
> times on this list,
i haven't seen it, or if i did see it, i don't remember :)

>The problem is not in the comparison itself, it is
>probably even happily performed,(snip) but the
>simpleton conditional does not know which way to
>jump when faced with a sequence.
>What is your advice it should do?
see above.

>- I hope this is not
> just another attempt to smuggle a case statement
>into Euphoria through the back door smile.
it isnt. really :)  honest :>

> My dislike of the compare() function is purely superficial: a
> comparison test should logically return TRUE when the objects are the
> same ('comparable') and FALSE when they are not.
i agree, wholeheartedly.  I was merely proffering a 'state of
mind' that i have found, for me, that made dealing with
that monster a tad easier.

new topic     » goto parent     » topic index » view message » categorize

5. Re: questions, questions; always questions :)

On Sun, 6 Sep 1998, Hawke wrote:

[Snippy. I like this thread, it's just that replies are getting very
large!]

> here's one that is TRUE:
>   -- x={200,432,56,618}    MaxX=640
>   -- y={112,252,35,460}    MaxY=480
>   -- if (x<MaxX) and (y<MaxY) and
>   --    (x>=0)   and (y>=0)   then
>   --    plot(x,y,GREEN)
>   -- end if
>   -- now look at the intermediate/final results:
>   -- if {1,1,1,1} and {1,1,1,1} and
>         {1,1,1,1} and {1,1,1,1} then
>         plot(x,y,GREEN) end if
>   -- if TRUE and TRUE and TRUE and TRUE then
>   -- if TRUE then   ---okay we do the if..then statement.
> no, i am not looking for 'case' here
> i am looking for =consistency=.

Easy!

Add some code and change the if statement like so:

constant True = 1, False = 0
-- I assume these are declare somewhere before.
-- They just make the code more intuitive.
-- Remember: True = not False and False = not True

function alltrue(sequence of_conditions)
    return not find(False, of_conditions)
end function

if alltrue(x<MaxX) and alltrue(y<MaxX) and
   alltrue(x>=0)   and alltrue(y>=0)   then

   -- etc.

end if

And the program's logic is still intact!

To be honest though, does the plot() statement really work like that?

The function allfalse() and how to use "*not* allwhatever()" are left as
exercises to the Euphoria community :)

Happy Coding,
Carl

--
Carl R White
E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :)
Url......: http://www.bigfoot.com/~cyrek/
In my last few days on the net, I have room for a witty quote. Irony. :S

new topic     » goto parent     » topic index » view message » categorize

6. Re: questions, questions; always questions :)

Carl R. White wrote:
> Add some code and change the if statement like so:
> function alltrue(sequence of_conditions)
>     return not find(False, of_conditions)
> end function
> if alltrue(x<MaxX) and alltrue(y<MaxX) and
>    alltrue(x>=0)   and alltrue(y>=0)   then
>    plot(x,y,GREEN)
> end if
another elegant solution... gotta admit i had to
read that one a couple times to 'catch it'...heh
'course, i've only had 1 cup o'java tis morn'

>To be honest though, does the plot() statement
>really work like that?
well, the plot() function doesn't exist as a
standard euphoria function, it would have
to be made.  I was just trying to provide
an inherently intuitive pseudo-function that
all of us would immediately know what the
function did. to specify:
   procedure plot(sequence x, sequence y, atom clr)
   --assumes x & y are both equal length sequences
   --containing valid points on the user graphic screen
   --note: this is prolly not the best implementation of
   --      this function...
        for index = 1 to length(x) do
                pixel(clr,{x[index],y[index])
        end for
   end procedure

> The function allfalse()
function allfalse(sequence conditions)
   return not find(TRUE,conditions)
end function
:)

>and how to use "*not* allwhatever()" are left as
>exercises to the Euphoria community :)
don't get that one... :(

take care all, and hope you get back online carl--Hawke'

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu