1. if statement not working
- Posted by xxmyt at yahoo.com
Mar 21, 2002
hello everyone.
the code i pasted down works properly. however, it doesnt work in the
second way.
my code:
include dos32lib.e
without warning
constant
Win = create( Window, "PASSWORD REQUIRED", 0, Default, Default,
400, 200, 0 ),
Sle1 = create( EditText, "", Win, 10, 40, 120, 20, 0 ),
Button1 = create( PushButton, "OK", Win, 180, 40, 120, 20, 0 )
procedure onClick_Button1()
if equal(getText(Sle1),"YES") then
closeWindow(Win)
end if
end procedure
onClick[Button1] = routine_id("onClick_Button1")
WinMain( Win )
but if i change the if statement this way, i get an error message:
if getText(Sle1) = "YES" then
closeWindow(Win)
end if
error message says, the result of the if statemnt should be an atom.
so whats it all about?
2. Re: if statement not working
This, in my not so humble opinion, is one of RDS's lesser decisions.
For some reason, not quiet explained yet, RDS decided that sequences must not
allowed to be compared
using the normal comparision operators. For sequences you are forced into
calling a function to
compare them. The two main functions are 'compare()' and 'equal()'.
compare(s1,s2) returns one of three values:
-1 if s1 is less than s2
0 if s1 equals s2
1 if s1 is greater than s2
equal(s1,s2) returns one of two values:
0 if s1 is not equal to s2
1 if s1 is equal to s2
Go figure! Many people have been try to get RDS to see sense in using the
standard comparision
operators with sequences but to no avail yet.
-------
Derek.
22/03/2002 11:04:33 AM, xxmyt at yahoo.com wrote:
>
>hello everyone.
>
>the code i pasted down works properly. however, it doesnt work in the
>second way.
>
>my code:
>include dos32lib.e
>without warning
>
>constant
> Win = create( Window, "PASSWORD REQUIRED", 0, Default, Default,
>400, 200, 0 ),
>
> Sle1 = create( EditText, "", Win, 10, 40, 120, 20, 0 ),
> Button1 = create( PushButton, "OK", Win, 180, 40, 120, 20, 0 )
>
>
>procedure onClick_Button1()
>
> if equal(getText(Sle1),"YES") then
> closeWindow(Win)
> end if
>
>end procedure
>
>onClick[Button1] = routine_id("onClick_Button1")
>
>WinMain( Win )
>
> but if i change the if statement this way, i get an error message:
>
> if getText(Sle1) = "YES" then
> closeWindow(Win)
> end if
>
>error message says, the result of the if statemnt should be an atom.
>
>so whats it all about?
>
>
>
>
---------
Cheers,
Derek Parnell
ICQ# 7647806
3. Re: if statement not working
Euphoria isn't C, or Pascal, or any other language. That's a good thing. But
it doesn't exist in a vaccum, either. And newbies (and myself) seem to be
tripped up by two features in Euphoria.
1. The '=' sign. I've argued against the current implementation until I was
blue in my face. I think the current behavior is more likely to result in
buggy code. In what other language does an '==' cause the program to come to
a hard stop?
The only good thing about this is that it prepares you for C coding, by using
compare() every time you deal with a string. After a while, it becomes second
nature.
But Robert sees this as a feature, and it's not going to change.
2. Using slices on strings. This "gotcha" was a handmine that littered a lot
of my early code. This initially looks like a good idea, until you get a
string shorter than you expected. So you write:
if s[1..3] = "/* "
and the code goes into cardiac arrest if the length of s is less than 3. If
nothing else, Euphoria should offer a substring function that would provide
some safety:
if substr( s, 1, 3 ) = "/* "
Of course, it would have to pad the string that's returned with nulls so the
length would match what was expected, or else *boom*.
Adding short-circuiting helps, but still, I think it's a nasty trap that
could easily turn someone away from using Euphoria.
-- David Cuny
4. Re: if statement not working
I wrote:
> This "gotcha" was a handmine that littered a lot of my early code.
Oooh, a mangled word that Yip Harburg would be proud of *and* a mixed
metaphor. My deepest apologies to all.
-- David Cuny
5. Re: if statement not working
22/03/2002 5:04:01 PM, David Cuny <dcuny at LANSET.COM> wrote:
>
>Euphoria isn't C, or Pascal, or any other language. That's a good thing. But
>it doesn't exist in a vaccum, either. And newbies (and myself) seem to be
>tripped up by two features in Euphoria.
>
>1. The '=' sign. I've argued against the current implementation until I was
>blue in my face. I think the current behavior is more likely to result in
>buggy code. In what other language does an '==' cause the program to come to
>a hard stop?
I can see where compare() came from. In 'C' one has to use strcmp() for a good
reason, because
strings, and arrays in general, are really pointers. So:
char *s, *t;
if (s == t) // actually checks to see if 's' and 't' are literally
addressing the same RAM
location.
but
if (strcmp(s,t) == 0) // is calling a function to examine each byte in the
character arrays
However, as David points out, Euphoria ain't 'C'. In Eu, there are no pointers.
So there can be no
confusion about '=' referring to either the address or contents of a sequence.
It has to be the
contents because you cannot reference its address.
>The only good thing about this is that it prepares you for C coding, by using
>compare() every time you deal with a string. After a while, it becomes second
>nature.
And of course, we should all be preparing to abandon Eu for C
>But Robert sees this as a feature, and it's not going to change.
Go figure!
>2. Using slices on strings. This "gotcha" was a handmine that littered a lot
>of my early code. This initially looks like a good idea, until you get a
>string shorter than you expected. So you write:
>
> if s[1..3] = "/* "
>
>and the code goes into cardiac arrest if the length of s is less than 3. If
>nothing else, Euphoria should offer a substring function that would provide
>some safety:
>
> if substr( s, 1, 3 ) = "/* "
>
>Of course, it would have to pad the string that's returned with nulls so the
>length would match what was expected, or else *boom*.
>
>Adding short-circuiting helps, but still, I think it's a nasty trap that
>could easily turn someone away from using Euphoria.
I just tripped against this stupid restriction again last night. I've written a
function called
begins() to help me out, but it ain't pretty.
if begins(s, t) then ...
I originally wrote:
if equal(s[1..length(t)], t) then ...
but this fails whenever s is shorter than t.
(I'm writting a simple text adventure game to highlight a number of new
additions to win32lib. If
things go well, I'll have it operational by the end of the weekend.)
-------
Derek.
6. Re: if statement not working
22/03/2002 5:56:29 PM, David Cuny <dcuny at LANSET.COM> wrote:
>
>I wrote:
>
>> This "gotcha" was a handmine that littered a lot of my early code.
>
>Oooh, a mangled word that Yip Harburg would be proud of *and* a mixed
>metaphor. My deepest apologies to all.
>
Hey! I loved it. I want to use it from now on. It is so graphic.
7. Re: if statement not working
>I just tripped against this stupid restriction again last night. I've
written a function called
>begins() to help me out, but it ain't pretty.
>
> if begins(s, t) then ...
>
>I originally wrote:
>
> if equal(s[1..length(t)], t) then ...
>
>but this fails whenever s is shorter than t.
Yeah, and I hate having to press enter at the end of the
line, it really annoys me. Rob can you fix this or I want my
money back.....
But seriously, having the interpreter return a valid result
from accessing non-existant data is just way stupid.
Graeme
8. Re: if statement not working
On 22 Mar 2002, at 18:03, Derek Parnell wrote:
<snip>
I just use equal() on everything now.
<snip>
> >2. Using slices on strings. This "gotcha" was a handmine that littered a lot
> >of
> >my early code. This initially looks like a good idea, until you get a string
> >shorter than you expected. So you write:
> >
> > if s[1..3] = "/* "
> >
> >and the code goes into cardiac arrest if the length of s is less than 3. If
> >nothing else, Euphoria should offer a substring function that would provide
> >some safety:
> >
> > if substr( s, 1, 3 ) = "/* "
> >
> >Of course, it would have to pad the string that's returned with nulls so the
> >length would match what was expected, or else *boom*.
> >
> >Adding short-circuiting helps, but still, I think it's a nasty trap that
> >could easily turn someone away from using Euphoria.
>
> I just tripped against this stupid restriction again last night. I've written
> a
> function called begins() to help me out, but it ain't pretty.
>
> if begins(s, t) then ...
>
> I originally wrote:
>
> if equal(s[1..length(t)], t) then ...
>
> but this fails whenever s is shorter than t.
This, to me, is a no-brainer. As much of s should be returned as possible, up
to length(t), so what if you ask for more than what is there? This, and the
equal() thing, is why i mentioned mirc yr(s) ago, even if you consider it a non-
language, it runs without crashing in that example. You toss enough of these
features at a newbie, and they will leave. I was just at a standstill in Pascal,
and C, and not getting anywhere with anything else at the time, i was
frustrated enough to stay with Eu longer than normal.
Kat
9. Re: if statement not working
----- Original Message -----
From: "Graeme" <graemeburke at hotmail.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: if statement not working
>
>
> >I just tripped against this stupid restriction again last night. I've
> written a function called
> >begins() to help me out, but it ain't pretty.
> >
> > if begins(s, t) then ...
> >
> >I originally wrote:
> >
> > if equal(s[1..length(t)], t) then ...
> >
> >but this fails whenever s is shorter than t.
>
>
> Yeah, and I hate having to press enter at the end of the
> line, it really annoys me. Rob can you fix this or I want my
> money back.....
>
Thanks for those encouraging words. I feel much better now that I realise I
must not desecrate the temple of Euphoria.
> But seriously, having the interpreter return a valid result
> from accessing non-existant data is just way stupid.
>
Graeme, you are absolutely correct.
10. Re: if statement not working
On Fri, 22 Mar 2002 02:07:16 -0600
Kat <gertie at PELL.NET> wrote: (reformatted)
-------------------------------------------------------------------------
-- You toss enough of these features at a newbie, and they will leave. --
-------------------------------------------------------------------------
> I was just at a standstill in Pascal,
> and C, and not getting anywhere with anything else at the time, i was
> frustrated enough to stay with Eu longer than normal.
>
> Kat
Thank you Kat. I don't know what it will take to get that simple message
into Rob's head, but it bears repeating until he realizes that Euphoria is
not his baby (to be loved, warts and all), but is instead a product to be
sold to *satisfied* customers.
Good as Euphoria is, there are some 'features' which are confusing and
discouraging to newcomers to programming(1), and just plain irritating to
experienced programmers.(2)
(1) Making them give up on Euphoria
(2) see (1)
Regards,
Irv
-------------------------------------------------------------------------
-- You toss enough of these features at a newbie, and they will leave. --
-------------------------------------------------------------------------
11. Re: if statement not working
Graeme wrote:
> But seriously, having the interpreter return a valid result
> from accessing non-existant data is just way stupid.
Is there really a need to make the attack so personal?
Euphoria is sequence based. On first contact with Euphoria, giddy with power
once you grasp the power of the sequence, it appears that using slices to get
substrings is the way to do things.
Euphoria doesn't offer any function for extracting substrings, so it
reinforces that notion. So the hapless newbie is led down the garden path,
writing stuff like:
if s[1..2] = "/*"
which initially works fine, and then one day appears to blow up for no reason.
Now, I know the 'proper' way to deal with this is to add a guard to the
statement:
if length(s) >= 2 and s[1..2] = "/*" then
but this is bad, because you've decoupled the size test from the actual
string. So if the string is changed later (and it will be) but the guard
accidentally left alone:
if length(s) >= 2 and s[1..3] = "-- " then
the result is unsafe code. And it'll pass tests just fine, until one day it
mysteriously crashes again.
Now, you could try writing:
if match( s, "/* " ) = 1 then
which is inefficient for large strings, but it's safe. The problem is, it
only works or finding the *first* occurance of the string. So these two are
*not* the same tests:
if match( s, "--" ) = 10
if length(s) >= 11 and s[10..11] = "--" then
To reiterate:
0. The goal is to write safe string handling code.
1. Slices (by themselves) are unsafe when working with strings.
2. Guarded slices are unsafe because the guard isn't coupled to the string.
3. Euphoria doesn't provide any built-in routines for creating substrings.
I think that extracting substrings is common enough operation - and one that
traps newbies and me far too often - that it deserves a builtin function:
s1 = substring( s2, n1, n2 )
which would behave the same as s2[n1,n2], except pad the string if n1 or n2 >
length(s2).
I'll grant that something like this would be more efficient for tests:
n1 = contains_substring( s1, n1, n2, s2 )
but I think the addition of the first would go a long ways toward making
safer Euphoria code.
-- David Cuny
12. Re: if statement not working
On 22 Mar 2002, at 23:24, Derek Parnell wrote:
>
>
> ----- Original Message -----
> From: "Graeme" <graemeburke at hotmail.com>
> To: "EUforum" <EUforum at topica.com>
> Sent: Friday, March 22, 2002 7:20 PM
> Subject: Re: if statement not working
>
>
> > >I just tripped against this stupid restriction again last night. I've
> > written a function called
> > >begins() to help me out, but it ain't pretty.
> > >
> > > if begins(s, t) then ...
> > >
> > >I originally wrote:
> > >
> > > if equal(s[1..length(t)], t) then ...
> > >
> > >but this fails whenever s is shorter than t.
> >
> >
> > Yeah, and I hate having to press enter at the end of the
> > line, it really annoys me. Rob can you fix this or I want my
> > money back.....
> >
>
> Thanks for those encouraging words. I feel much better now that I realise I
> must
> not desecrate the temple of Euphoria.
>
> > But seriously, having the interpreter return a valid result
> > from accessing non-existant data is just way stupid.
> >
>
> Graeme, you are absolutely correct.
So if i were to ask you to return all 100K bytes of this email, you'd land in a
mental institution? Eu would, figuratively speaking.
Kat
13. Re: if statement not working
-------Phoenix-Boundary-07081998-
Hi David Cuny, you wrote on 3/22/02 7:04:39 AM:
>To reiterate:
>
>0. The goal is to write safe string handling code.
>1. Slices (by themselves) are unsafe when working with strings.
>2. Guarded slices are unsafe because the guard isn't coupled to the string.
>3. Euphoria doesn't provide any built-in routines for creating substrings.
>
How about a new operator '=~' that acts like '=' except that it
plows on ahead in the face of errors:
s =~ t[2..4]
-- s is padded to length 3 if necessary
if s[2..4] =~ '123' then
-- comparison fails if s is too short
-- (or uninitialized!)
I suppose the postfix '~' could also be applied elsewhere:
s = t[2..8]~ --s has 6 elements even if t has 4
But I haven't thought too much about that.
Karl Bochert
-------Phoenix-Boundary-07081998---
14. Re: if statement not working
At 07:04 AM 3/22/02 -0800, you wrote:
>
>Graeme wrote:
>
>> But seriously, having the interpreter return a valid result
>> from accessing non-existant data is just way stupid.
>
>Is there really a need to make the attack so personal?
Well that's EXACTLY what i thought.....
Sorry David. There was nothing of a personal nature in my
post, unless you mean the word 'stupid'. I used it because
its the same word Derek used to describe his opinion in the
post I was replying to. I quoted his comment in my post. You
have removed this from the top of your reply, as I'm sure you
must be aware.
>So if i were to ask you to return all 100K bytes of this
>email, you'd land in a mental institution? Eu would,
>figuratively speaking.
>Kat
So if you asked me for $100 and I diddnt have it so i gave
you 3 $10 notes and the rest in monopoly money that would
be ok?
AND HERES THE REAL PROBLEM --->
What if you dont notice, then you go to the supermarket and
and try to pay for your groceries? Will the cashier accept
the monopoly money? Maybe. Hopefull not, because every time
somebody accepts it, the bug is one step further down the line,
and when it enevtaully stops, you've got a hell of a mess trying
to figure out who's handing out the bad cash...
Graeme.
15. Re: if statement not working
On 22 Mar 2002, at 9:47, kbochert at ix.netcom.com wrote:
>
> Hi David Cuny, you wrote on 3/22/02 7:04:39 AM:
>
> >To reiterate:
> >
> >0. The goal is to write safe string handling code.
> >1. Slices (by themselves) are unsafe when working with strings.
Before someone nails down "type string", lets make all sequence handling
safe, regardless of what's stored in it.
> >2. Guarded slices are unsafe because the guard isn't coupled to the string.
> >3.
> >Euphoria doesn't provide any built-in routines for creating substrings.
Unless you parse() the string (err, sequence) and such. Really, i mix what i
think most people call "string" with nested sequences. I'd hate to see
restrictions put on what can or cannot be a "string", Eu's strength is the
sequence. For instance, if you nail down a string to be atoms of 1-255, you
can't handle most of the world's language characters gracefully (old thread
from yrs ago). This would hurt translation dictionaries for one, and could
artificially restrict the countries that use Eu, as well as the knowledge
domains that use it (like all those calculus and trig symbols).
> How about a new operator '=~'
First thought: ~ like "fuzzy logic"? Hmmm, better write that as a real optional
include at this point, Rob will never sign onto it.
> that acts like '=' except that it
> plows on ahead in the face of errors:
> s =~ t[2..4]
> -- s is padded to length 3 if necessary
Since you specified [2..3], why pad s some more? And with what? Better to
look at what t actually has, and deliver what is in the envelope of [2..3] and
no more. If [2..3] doesn't exist, then return {}. If you asked to borrow a book
that i don't have, i won't pad out a empty box of non-existant book with some
random selection of book parts.
> if s[2..4] =~ '123' then
> -- comparison fails if s is too short
> -- (or uninitialized!)
In this case, if s is too short, it's not equal. You did a test there, not an
extraction or assignment, it obviously fails, but shouldn't bomb the whole
program. In mirc, if i do:
if ( $gettok(%address,2,$asc(.)) == ipt ) { whatever }
and %address has nothing in it this time, because of whatever, i expect this
boolean test to fail, not the entire program. In Eu, i expect the whole program
to crash horribly.
> I suppose the postfix '~' could also be applied elsewhere:
> s = t[2..8]~ --s has 6 elements even if t has 4
> But I haven't thought too much about that.
Nononononono! In this case, if t has 6 elements, Eu should grab the [2..6] of
them for s, making s have 5 elements.
Basically, if Eu would crash on it because of a length, either preemptively
handle it, or grab the error Eu throws now, and deal with it intelligently.
Don't
add anything to it, because whatever you add might be tested for later on
and cause an error.
Kat
16. Re: if statement not working
On 23 Mar 2002, at 7:45, Graeme wrote:
<snip>
> >So if i were to ask you to return all 100K bytes of this
> >email, you'd land in a mental institution? Eu would,
> >figuratively speaking.
>
> >Kat
>
>
> So if you asked me for $100 and I diddnt have it so i gave
> you 3 $10 notes and the rest in monopoly money that would
> be ok?
No. The monopoly money would be padding, which i am against. I would not
want you to crash and burn because i asked you for $100 and you had only
$99, or had $100 in Chinese currency. I'd expect you to hand over the 3 $10
notes, if you don't mind, thanks!
> AND HERES THE REAL PROBLEM --->
>
> What if you dont notice, then you go to the supermarket and
> and try to pay for your groceries? Will the cashier accept
> the monopoly money? Maybe. Hopefull not, because every time
> somebody accepts it, the bug is one step further down the line,
> and when it enevtaully stops, you've got a hell of a mess trying
> to figure out who's handing out the bad cash...
So stop with the padding already! When can i expect that $30?
Kat
17. Re: if statement not working
-------Phoenix-Boundary-07081998-
Hi Kat, you wrote on 3/22/02 2:50:39 PM:
>> How about a new operator '=~'
>First thought: ~ like "fuzzy logic"? Hmmm, better write that as a real
>optional include at this point, Rob will never sign onto it.
We already know that Rob will sign on to almost nothing
I think using an operator to provide this option is both more
efficient and safer than using a new type. (not to mention easier).
I was going to use '==' but then '==' and '=' seem backwards to
me.
There is great scope for artistry here-- how about (=) or
>> that acts like '=' except that it
>> plows on ahead in the face of errors:
>> s =~ t[2..4]
>> -- s is padded to length 3 if necessary
>Since you specified [2..3], why pad s some more? And with what?
> Better to look at what t actually has, and deliver what is in
> the envelope of [2..3] and no more. If [2..3] doesn't exist,
> then return {}. If you asked to borrow a book that i don't have,
> i won't pad out a empty box of non-existant book with some
> random selection of book parts.
If I ask you for a book and you are missing the last chapter, I
might appeciate getting what you have.
Your [2..3] confused me, so to make the sample clearer:
sequence t = "abcd"
sequence s = t[3..6]
I can see 4 possible results:
1) ""
2) "cd"
3) "cd "
4) Fatal Error
I like 2), i.e. do the best you can.
>> if s[2..4] =~ '123' then
>> -- comparison fails if s is too short
>> -- (or uninitialized!)
>
>In this case, if s is too short, it's not equal. You did a test
> there, not an extraction or assignment, it obviously fails,
> but shouldn't bomb the whole program.
By 'fails', I meant that the boolean test fails (returns false)
>> I suppose the postfix '~' could also be applied elsewhere:
>> s = t[2..8]~ --s has 6 elements even if t has 4
>> But I haven't thought too much about that.
>
>Nononononono! In this case, if t has 6 elements, Eu should
> grab the [2..6] of them for s, making s have 5 elements.
With a moment's reflection, I agree.
Karl Bochert
-------Phoenix-Boundary-07081998---
18. Re: if statement not working
I'm sorry. I've had a decent sleep now. Yesterday was not a good day at the
office.
----- Original Message -----
From: "Graeme" <graemeburke at hotmail.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: if statement not working
>
> At 07:04 AM 3/22/02 -0800, you wrote:
> >
> >Graeme wrote:
> >
> >> But seriously, having the interpreter return a valid result
> >> from accessing non-existant data is just way stupid.
> >
> >Is there really a need to make the attack so personal?
>
>
> Well that's EXACTLY what i thought.....
>
> Sorry David. There was nothing of a personal nature in my
> post, unless you mean the word 'stupid'. I used it because
> its the same word Derek used to describe his opinion in the
> post I was replying to. I quoted his comment in my post. You
> have removed this from the top of your reply, as I'm sure you
> must be aware.
>
And Graeme, you are absolutely correct. I was angry over some poor decisions
that colleagues of mine had made; committing my time to projects interstate
for months on end, without consulting me, or my bosses. I've calmed down a
lot now.
>
> >So if i were to ask you to return all 100K bytes of this
> >email, you'd land in a mental institution? Eu would,
> >figuratively speaking.
>
> >Kat
>
>
> So if you asked me for $100 and I diddnt have it so i gave
> you 3 $10 notes and the rest in monopoly money that would
> be ok?
>
> AND HERES THE REAL PROBLEM --->
>
> What if you dont notice, then you go to the supermarket and
> and try to pay for your groceries? Will the cashier accept
> the monopoly money? Maybe. Hopefull not, because every time
> somebody accepts it, the bug is one step further down the line,
> and when it enevtaully stops, you've got a hell of a mess trying
> to figure out who's handing out the bad cash...
>
It is good that Euphoria doesn't allow you access to non-existant sequence
elements. Thus :
s = "abc"
t = s[1..4]
Eu is proper to reject this.
My problem still remains though, and I don't have a reasonable solution yet.
Simply put, I don't care how many elements the sequences have, all I want to
know is : does one object begin with another object? I suppose the simplest
approach is:
s = upper(trim(t))
if match("*AUTHOR ", s) = 1 then
GameInfo[vAuthor] = t[9.. length(t)]
...
elsif match("*COPYRIGHT ", s) = 1 then
GameInfo[vCopyright] = t[12.. length(t)]
...
elsif match("*TITLE ", s = 1) then
GameInfo[vTitle] = t[8.. length(t)]
...
However, in my never-ending quest for readable code, the phrase "match(a,b)
= 1" is not obviously telling the reader that I'm trying to see if 'a'
begins with 'b'. That's part of the ambiguitity of using literal numbers in
code; you don't know if they are special (magic) numbers or not. So I could
make it a bit better by doing:
constant beginning = 1
. . .
if match("*AUTHOR ", s) = beginning then
but that looks a bit ambiguous too. Or :
if begins(s, "*AUTHOR ") then
but that sounds unusual if you say it out loud.
What I'd really, really like to write is something like:
structure GameInfo
sequence Title
sequence Author
sequence Copyright
end structure
GameInfo theGame
. . .
case upper(trim(t)) do
when begins "*AUTHOR " then
theGame.Author = t[9..$]
...
when begins "*COPYRIGHT " then
theGame.Copyright = t[12..$]
...
when begins "*TITLE " then
theGame.Title = t[8..$]
...
end case
but I know that ain't going to happen
-----------
cheers,
Derek
19. Re: if statement not working
-------Phoenix-Boundary-07081998-
Hi Derek Parnell, you wrote on 3/22/02 5:48:27 PM:
>However, in my never-ending quest for readable code, the phrase "match(a,b)
>= 1" is not obviously telling the reader that I'm trying to see if 'a'
>begins with 'b'. That's part of the ambiguitity of using literal numbers in
>code; you don't know if they are special (magic) numbers or not. So I could
>make it a bit better by doing:
>
> constant beginning = 1
> . . .
> if match("*AUTHOR ", s) = beginning then
>
>but that looks a bit ambiguous too. Or :
>
> if begins(s, "*AUTHOR ") then
>
>but that sounds unusual if you say it out loud.
>
I think
if begins_with (s, "*AUTHOR ") then
is self-explanatory.
Or how about another oddball operator (infix function??)
if s <begins_with> "*AUTHOR " then
!?
Karl Bochert
-------Phoenix-Boundary-07081998---
20. Re: if statement not working
----- Original Message -----
From: <kbochert at ix.netcom.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: if statement not working
>I think
>
> if begins_with (s, "*AUTHOR ") then
>
>is self-explanatory.
Maybe, but is this asking if s begins with "*AUTHOR ", or "*AUTHOR " begins
with s ? It is not so unambiguous really.
>Or how about another oddball operator (infix function??)
>
> if s <begins_with> "*AUTHOR " then
I like the idea of infix functions. I notice in the D news-group this is
sometimes discussed. It must be a real pain for interpreter/compiler writers
to do well though.
PreFix functions are where the function name appears to the left of its
parameters, infix functions have parameters before and after the function
name, and postfix functions have their parameters on the lefthand side.
Prefix: add a b
Infix: a add b
postfix: a b add
Most languages support prefix functions, some post fix (eg Forth) , but
rarely infix. Infix is usually only reserved for operators such as + and *.
So how about something like :
infix function begins_with( object x : object y )
return (match(y,x) = 1)
end function
if a begins_with b then
. . .
end if
Just a thought
-----------
Derek
21. Re: if statement not working
- Posted by rforno at tutopia.com
Mar 22, 2002
David:
if s[1..2] = "/*" then ...
could never work fine. It gives an error message telling conditions must be
atoms.
----- Original Message -----
From: "David Cuny" <dcuny at LANSET.COM>
To: "EUforum" <EUforum at topica.com>
Subject: Re: if statement not working
Graeme wrote:
> But seriously, having the interpreter return a valid result
> from accessing non-existant data is just way stupid.
Is there really a need to make the attack so personal?
Euphoria is sequence based. On first contact with Euphoria, giddy with power
once you grasp the power of the sequence, it appears that using slices to
get
substrings is the way to do things.
Euphoria doesn't offer any function for extracting substrings, so it
reinforces that notion. So the hapless newbie is led down the garden path,
writing stuff like:
if s[1..2] = "/*"
which initially works fine, and then one day appears to blow up for no
reason.
Now, I know the 'proper' way to deal with this is to add a guard to the
statement:
if length(s) >= 2 and s[1..2] = "/*" then
but this is bad, because you've decoupled the size test from the actual
string. So if the string is changed later (and it will be) but the guard
accidentally left alone:
if length(s) >= 2 and s[1..3] = "-- " then
the result is unsafe code. And it'll pass tests just fine, until one day it
mysteriously crashes again.
Now, you could try writing:
if match( s, "/* " ) = 1 then
which is inefficient for large strings, but it's safe. The problem is, it
only works or finding the *first* occurance of the string. So these two are
*not* the same tests:
if match( s, "--" ) = 10
if length(s) >= 11 and s[10..11] = "--" then
To reiterate:
0. The goal is to write safe string handling code.
1. Slices (by themselves) are unsafe when working with strings.
2. Guarded slices are unsafe because the guard isn't coupled to the string.
3. Euphoria doesn't provide any built-in routines for creating substrings.
I think that extracting substrings is common enough operation - and one that
traps newbies and me far too often - that it deserves a builtin function:
s1 = substring( s2, n1, n2 )
which would behave the same as s2[n1,n2], except pad the string if n1 or n2
>
length(s2).
I'll grant that something like this would be more efficient for tests:
n1 = contains_substring( s1, n1, n2, s2 )
but I think the addition of the first would go a long ways toward making
safer Euphoria code.
-- David Cuny
22. Re: if statement not working
On 22 Mar 2002, at 17:07, kbochert at ix.netcom.com wrote:
> -------Phoenix-Boundary-07081998-
> Content-type: text/plain; charset=ISO-8859-1
> Content-transfer-encoding: quoted-printable
<snip>
> I was going to use '=3D=3D' but then '=3D=3D' and '=3D' seem backwards to
> me.
> There is great scope for artistry here-- how about (=3D) or
lol !
> >> that acts like '=3D' except that it
> >> plows on ahead in the face of errors:
> >> s =3D~ t[2..4]
> >> -- s is padded to length 3 if necessary
>
> >Since you specified [2..3], why pad s some more? And with what?
> > Better to look at what t actually has, and deliver what is in
> > the envelope of [2..3] and no more. If [2..3] doesn't exist,
> > then return {}. If you asked to borrow a book that i don't have,
> > i won't pad out a empty box of non-existant book with some
> > random selection of book parts.
>
> If I ask you for a book and you are missing the last chapter, I
> might appeciate getting what you have.
Yes. I surely would not glue in enough pages from other books to make up
the length of the original book, heh.
> Your [2..3] confused me, so to make the sample clearer:
> sequence t =3D "abcd"
> sequence s =3D t[3..6]
> I can see 4 possible results:
> 1) ""
> 2) "cd"
> 3) "cd "
> 4) Fatal Error
>
> I like 2), i.e. do the best you can.
I agree. Nothing else makes sense in safe processing. It also take care of
the req for "inf" as predefined constant, because defining it like:
constant len len = 500000000000000
s = "123456789"
?s[5..len] -- "56789"
(you know what i mean)
Kat
23. Re: if statement not working
rforno wrote:
> if s[1..2] = "/*" then ...
> could never work fine. It gives an error message telling conditions must be
> atoms.
Yes, and that's perhaps the most suprising bit of all. You'd sort of expect
that with an emphasis on being orthagonal, the 'if' would accept {0, 0} as
false, and { 0, 1 } as true.
-- David Cuny
24. Re: if statement not working
At 06:10 PM 3/22/02 -0600, Kat wrote:
>
>So stop with the padding already! When can i expect that $30?
>
so if s="hello"
s[2..4] is "ell"
s[-1..2] is "he"
s[5..6] is "o"
and s[34..39] is {}
interesting idea.
I still dont like it that much...
.. but I would say that wouldn't I? ;)
Graeme.
25. Re: if statement not working
On Sat, 23 Mar 2002 01:28:21 -0800, David Cuny <dcuny at LANSET.COM>
wrote:
>
>rforno wrote:
>
>> if s[1..2] = "/*" then ...
>> could never work fine. It gives an error message telling conditions must be
>> atoms.
>
>Yes, and that's perhaps the most suprising bit of all. You'd sort of expect
>that with an emphasis on being orthagonal, the 'if' would accept {0, 0} as
>false, and { 0, 1 } as true.
>
A slight typo there I think. Surely if s is "//" or "**" then the
comparison would return {1,0} or {0,1} respectively. You meant {1,1}.
If every element of a sequence is a +ve integer, if treats it as true.
Question: what does this print?
a={}
if a then ?1 else ?0 end if
Pete
PS if Rob completely annihilates all index bounds checking, which I
doubt, then fine, I have the source so I can put it back!
26. Re: if statement not working
-------Phoenix-Boundary-07081998-
Hi Derek Parnell, you wrote on 3/22/02 7:25:57 PM:
>I think
>
> if begins_with (s, "*AUTHOR ") then
>
>is self-explanatory.
>
>Maybe, but is this asking if s begins with "*AUTHOR ", or "*AUTHOR " begins
>with s ? It is not so unambiguous really.
>Or how about another oddball operator (infix function??)
>
> if s <begins_with> "*AUTHOR " then
>
>I like the idea of infix functions. I notice in the D news-group this is
>sometimes discussed. It must be a real pain for interpreter/compiler
>writers to do well though.
>
>So how about something like :
>
> infix function begins_with( object x : object y )
> return (match(y,x) = 1)
> end function
>
> if a begins_with b then
> . . .
> end if
>
Doesn't seem too difficult, especially if the 'operator' is
set off with a special character, a situation I like anyway
because it serves to emphasize that the id is an operator:
len = a <max> b
I suppose it would be restricted to being a function, and taking
two arguments.
In addition, it would pave the way for a whole series of built-in
'operators' that say what they do.
Karl Bochert
-------Phoenix-Boundary-07081998---
27. Re: if statement not working
-------Phoenix-Boundary-07081998-
You wrote on 3/23/02 10:17:33 AM:
>Pete
>PS if Rob completely annihilates all index bounds checking, which I
>doubt, then fine, I have the source so I can put it back!
>
Me too! But it would be nice to be able to respond to a bounds
violation with something other than a fatal abort.
Karl Bochert
-------Phoenix-Boundary-07081998---
28. Re: if statement not working
Rod Jackson wrote:
>> Yes, and that's perhaps the most suprising bit of all. You'd sort of
>> expect that with an emphasis on being orthagonal, the 'if' would
>> accept {0, 0} as false, and { 0, 1 } as true.
>
> Well, I'm kinda late into this thread, but I have to ask:
>
> Why would {0,1} be logically assumed to be true?
No, sorry. For some reason all zeros as false and anything else as true
seemed like a good idea last night, but it seems pretty indefensible now.
In any event, I was more musing out loud than actually advocating this.
The only thing I'm *actually* suggesting is that some string specific
routines (such as substring) be added to the standard Euphoria library. That
way, people would use them for string operations, instead of the more risky
slices.
-- David Cuny
29. Re: if statement not working
On Sat, 23 Mar 2002 10:43:18 -0800, kbochert at ix.netcom.com wrote:
>
>You wrote on 3/23/02 10:17:33 AM:
>
>>Pete
>>PS if Rob completely annihilates all index bounds checking, which I
>>doubt, then fine, I have the source so I can put it back!
>>
>
>Me too! But it would be nice to be able to respond to a bounds
>violation with something other than a fatal abort.
>
Agreed, ability to catch it would be a nice plus, ignoring it
wholesale not. However, not catched, it must give a fatal abort.
I liken this argument to swatting a fly with a howitzer.
It *IS* difficult telling the dumb computer what you really want:
firstnonspace=1
while x[firstnonspace]=' ' do firstnonspace+=1 end while
while 1 do
i = match({' ',9},x[firstnonspace..length(x)])
if i then
i+=firstnonspace-1
x=x[1..i-1]&x[i+1..length(x)]
end if
...
But that is *EXACT*; put a fudge factor in there & I'm really lost.
I'll second any motion for shorthand length(X) but I have yet to see
one which passes muster.
I'll accept, if s="123" then slice operands could make sense in the
bounds -3,-2,-1,0,1,2,3 (if agreed handling worked out),
*BUT NOT* s[37789456..-327] blithely returning {}. **NONSENSE**
Oh dear, and I tried so hard to stay clear of all this
Pete
PS. I've said before: code it & get it working in euphoria,
not wait for/beg Rob to do it in C/v2.4.
If not, why not?
30. Re: if statement not working
-------Phoenix-Boundary-07081998-
You wrote on 3/23/02 5:53:37 PM:
>It *IS* difficult telling the dumb computer what you really want:
>
> firstnonspace=1
> while x[firstnonspace]=' ' do firstnonspace+=1 end while
> while 1 do
> i = match({' ',9},x[firstnonspace..length(x)])
> if i then
> i+=firstnonspace-1
> x=x[1..i-1]&x[i+1..length(x)]
> end if
>....
i.e.
regex_find ("\b[^ 9]*", x) -- find the pattern
x = regex_head & regex_tail -- leave out the match
....
This kind of thing is easier with regular expressions.
Karl Bochert
-------Phoenix-Boundary-07081998---