[python-win32] A Suggestion for Python Syntax

Hung, Le Khanh softex@hn.vnn.vn
Sun, 3 Nov 2002 10:36:16 +0700


This is a multi-part message in MIME format.

------=_NextPart_000_0042_01C28324.D7089380
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

We all know that the statement like continue, break, .. are not =
structured.
But they are useful for flexible control of the execution. It is mainly
because of loop structure limitations.
Here we suggest a strengthened loop and selective constructions. With =
them
we need not to use break statement at all.


CompoudStatement ::=3D dostatement | loop statement
dostatement::=3D
    "do" suite
    GuardedCommand
loopstatement::=3D
    "loop" suite
    GuardedCommand
GuardedCommand::=3D

    ( ?[!] "check" Condition  suite )*
Condition ::=3D expression :
Condition ::=3D
    ( "case" Condition  suite )*
These constructions could replace all Python's compound statements

Some examples:
1. Python command
if B1 : S1
elif B2 : S2
else S3

is equivalent to

do
?! check case B1 : S1
              case B2 : S2
        S3
2. Python command
while B : S
is equivalent to
loop
? check B : S

3. # Search in Array
# This is common sketch for searching algorithms
InitSearchScope
loop
? check NotEmptySearchScope:
        TakeAnElement
? check NotFit:
        SrinkSearchScope

# Implementation of Sequential Search
i =3D lo;  # Scope is from lo to hi
loop
? check i < hi : # i is in Scope
? check a[i] !=3D x :
        i+=3D1

# Binary Search
i,j =3D lo,hi
loop
? check i<j : k =3D (i+j) / 2
? check
        case a[k]<x : i =3D k+1
        case a[k]>x : j =3D k-1

This bit of code has the same structure as the previous!

# For comparison, here is Python's Binary search
i,j =3D lo,hi
while i<j : k =3D (i+j) / 2
        if a[k]<x : i =3D k+1
        elif a[k]>x : j =3D k-1
        else break

It is not obvious and natural here that there are two exit points from =
the loop.
I would be happy to receive comments
Best regards.
Hung


------=_NextPart_000_0042_01C28324.D7089380
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>We all know that the statement like =
continue,=20
break, .. are not structured.<BR>But they are useful for flexible =
control of the=20
execution. It is mainly<BR>because of loop structure =
limitations.<BR>Here we=20
suggest a strengthened loop and selective constructions. With them<BR>we =
need=20
not to use break statement at all.<BR><BR><BR>CompoudStatement ::=3D =
dostatement |=20
loop statement<BR>dostatement::=3D<BR>&nbsp;&nbsp;&nbsp; "do"=20
suite<BR>&nbsp;&nbsp;&nbsp;=20
GuardedCommand<BR>loopstatement::=3D<BR>&nbsp;&nbsp;&nbsp; "loop"=20
suite<BR>&nbsp;&nbsp;&nbsp;=20
GuardedCommand<BR>GuardedCommand::=3D<BR><BR>&nbsp;&nbsp;&nbsp; ( ?[!] =
"check"=20
Condition&nbsp; suite )*<BR>Condition ::=3D expression :<BR>Condition=20
::=3D<BR>&nbsp;&nbsp;&nbsp; ( "case" Condition&nbsp; suite )*<BR>These=20
constructions could replace all Python's compound =
statements<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Some examples:<BR>1. Python=20
command<BR><STRONG>if</STRONG> B1 : S1<BR><STRONG>elif</STRONG> B2 :=20
S2<BR><STRONG>else</STRONG> S3<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>is equivalent =
to<BR><BR><STRONG>do</STRONG><BR>?!=20
<STRONG>check</STRONG> <STRONG>case</STRONG> B1 :=20
S1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;=20
<STRONG>case</STRONG> B2 : =
S2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
S3<BR>2. Python command<BR><STRONG>while</STRONG> B : S<BR>is equivalent =

to<BR><STRONG>loop</STRONG><BR>? <STRONG>check</STRONG> B : =
S<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>3. # Search in Array<BR># This is =
common sketch for=20
searching algorithms<BR>InitSearchScope<BR><STRONG>loop</STRONG><BR>?=20
<STRONG>check</STRONG>=20
NotEmptySearchScope:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
TakeAnElement<BR>? <STRONG>check</STRONG>=20
NotFit:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
SrinkSearchScope<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2># Implementation of Sequential =
Search<BR>i =3D=20
lo;&nbsp; # Scope is from lo to hi<BR><STRONG>loop</STRONG><BR>?=20
<STRONG>check</STRONG> i &lt; hi : # i is in Scope<BR>? =
<STRONG>check</STRONG>=20
a[i] !=3D x :<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
i+=3D1<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2># Binary Search<BR>i,j =3D=20
lo,hi<BR><STRONG>loop</STRONG><BR>? <STRONG>check</STRONG> i&lt;j : k =
=3D (i+j) /=20
2<BR>? =
<STRONG>check</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<STRONG>case</STRONG> a[k]&lt;x : i =3D=20
k+1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <STRONG>case</STRONG>=20
a[k]&gt;x : j =3D k-1<BR><BR>This bit of code has the same structure as =
the=20
previous!<BR><BR># For comparison, here is Python's Binary search<BR>i,j =
=3D=20
lo,hi<BR><STRONG>while</STRONG> i&lt;j : k =3D (i+j) /=20
2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <STRONG>if</STRONG> =
a[k]&lt;x :=20
i =3D k+1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
<STRONG>elif</STRONG>=20
a[k]&gt;x : j =3D k-1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<STRONG>else</STRONG> <STRONG>break</STRONG><BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>It is not <EM>obvious</EM> and =
<EM>natural</EM>=20
here that there are two exit points from the loop.<BR>I would be happy =
to=20
receive comments<BR>Best regards.<BR>Hung<BR></DIV></FONT></BODY></HTML>

------=_NextPart_000_0042_01C28324.D7089380--