Re: [If/then and sequences...]
From: Derek Parnell <derekp at SOLACE.COM.AU>
> In a lot of programming languages, the operators '<', '>' and '=' always
> return a boolean result - that is either TRUE or FALSE.
in most languages, actually, you dont have sequences, and its not allowed
to perform the actions that we do in euphoria, that are oft taken for
granted,
upon entire arrays...
unless u go thru painstaking for loops examining every array element...
eg:
sequence ages, teens
ages = {10,20,22,12,32,55,34,23,15}
function IsTeenager(sequence data)
return (data >=13) and (data <=19)
end function
teens = IsTeenager(ages)
now u have a nice neat list of 0's and 1, in a sequence, showing u the
indexes of all the teenagers, and the find() command will neatly locate
those indices...what could be simpler?
try that in pascal or C, and you are looking at for loops, bunches of
temporary storage array/variable declarations, and many lines of code...
>What you've demonstated is that in Euphoria, these operators return
>a boolean when both operands are atoms, and return a sequence
>when either operand is a sequence
ERRRRRRRRRRRRR
it returns a sequence OF booleans, that matches the original size and depth
of the original sequence
atoms beget a single 'atomic' boolean result
sequences beget a sequence of boolean results...
seems logical to me :)
> (unless the operand is used in an IF statement, in which it is illegal).
because there is no way to determine WHICH boolean within the sequence
you want to look at...
if u SPECIFY which boolean you want to look at within a sequence, by
referencing it SPECIFICALLY, then it works just fine...
eg: same as above
if ages = 12 then puts(1,"almost a teenager") end if
like...DOH, wont work...which person are you talking about???????
if ages[3] = 12 then puts(1,"almost a teenager") end if
well kick butt! now we know what person we are talking about, and
everything is kosher and still maintains absolute logicality....
>And that compare() function always returns an atom
of course, because we need a method which will allow sorting...
all compare() does is recursively analyze the results of a boolean
logic operation which was recursively applied to a sequence,
just like the example above, and neatly sends back a singular
boolean result instead of the list of results.
saves us having to actually thread a for loop or a find() function...
does it faster usually too cuz rob can take shortcuts...
and in reality, its more like a tri-state boolean that gets returned, not an
atom...
>(and equal() function always returns a boolean)
function equal(object a, object b)
return compare(a,b) = 0
end function
that's all rob is doing...it was an include to the core language for
conveince
sake, nothing more, nothing less....it makes code a touch cleaner, and
everyone
was always putting that function i just wrote into their code toolbox
anyway...
saves a wee typing...shrug...no magic here
>In my mind that is inconsistent and also not simple.
function IsTeenager(sequence data)
return (data >=13) and (data<=19)
end function
what is not simple about that function ????
where is the inconsistency???
seems quite clear and consistent to me?!?!?!?!
> Its just that from my point of view, it would have been more intuitive,
and
> simpler, to reverse this arrangement of Robert's. That is, have the '<',
> '>', and '=' operators always return a boolean (and allow their use in
IFs),
> and have the compare() function return a sequence.
wanna try writing IsTeenager useing that proposed method???
and have it return a list of boolean values that are exactly nested
as the passed sequences??????
something like this i suppose???
function IsTeenager(object data)
if atom(data) then return data end if
for i = 1 to length(data) do
if sequence(data[i]) then
data[i] = IsTeenager(data[i])
end if
if (data[i] >= 13) and (data[i]<=19) then
data[i] = 1
else data[i] = 0
end if
end for
return data
end function
hrmmmm, not so clear now????
hell, i aint even sure that's a fully perfect function to do the task...
i aint tested it, and i would have to test it to make sure it works...
but i ***KNOW*** from just looking at it, without a doubt,
what the following code does:
function IsTeenager(sequence data)
return (data >=13) and (data<=19)
end function
shrug, i prefer knowing with a glance what code does...
and the second function really looks a lot less complicated to me...
>This probably would have
>made the equal() function redunant. Of course its WAY too late to introduce
>this sort of change to the language, and I'm not advocating that. I'm just
>wondering why Robert decided to arrange the language the way he did.
see above????
> Current:
> Operation Operands Allowed in IF Result
> -------------------------------------------------------------------
> =/>/< atoms yes boolean
> =/>/< sequences no sequence
errrr NO....
that should read:
=/>/< sequences no (without proper referenced indicies) sequence
of booleans
> =/>/< mixed no sequence
what is 'mixed'?????
we have no 'mixed' data type in EU...
it is an integer, an atom, a sequence, or an object...
> compare() atoms yes atom
> compare() sequences yes atom
> compare() mixed yes atom
errrrr more properly, that would be
compare() atoms yes tristate boolean
ditto for sequences....logical and consistant
> equal() atoms yes boolean
> equal() sequences yes boolean
> equal() mixed yes boolean
tis only a shortcut for typing, as per above, and does no magic...
> if atom yes boolean
> if sequence no n/a
as per above, u cannot determine WHICH part of the sequence
you are looking at without telling the interpreter which part of
the sequence you are referring to...
to sum up, EU's grace and elegance comes from the
manner rob has defined the language as it stands...
lets suppose you want a function that examines any valid
pair euphoria objects, atoms or sequences, no matter how big or
nested they might be and determines which of them is the
smallest, or the minimum, persay...
to wit:
constant
first ={10,20,30,20,10},
second={20,10,40,10,20}
sequence result
result = minimum(first,second)
--result would now be = {10,10,30,10,10}
a function like this is extremely handy for fuzzy logic and
graphics applications
the function to do this was written long ago with one line
of euphoria code, and is as simple and elegant as they come
(by lucius i believe)
function minimum(object a, object b)
return a*(a<=b) + b*(b<a)
end function
i dont believe it gets much simpler or clearer then this in a language...
if you really really MUST have your greater() and lesser() functions
then try these:
function greater(object a, object b)
return compare(a,b) = 1
end function
function lesser(object a, object b)
return compare(a,b) = -1
end function
problem solved?????
hope this helps clear things up as to why EU does things the way
it does, and further elaborates on Irv's post as to why things
function they way they do in EU...
--Hawke'
_____NetZero Free Internet Access and Email______
http://www.netzero.net/download/index.html
|
Not Categorized, Please Help
|
|