At 10:00 AM 3/2/2009 -0800, michel paul wrote:

>Before I discovered Python a couple of years ago I was experimenting with a 
>pseudo-code approach for expressing math concepts.  I had this kind of stuff 
>in mind:
>
>factorial(n):
>    if n < 2 ---> 1
>    else ---> n*factorial(n-1)

I like the feeling of action in --->, but I also like the self-explanatory 
"return".  Any other suggestions?  Strong preferences?

We could use a symbol other than = for assignment, just to avoid confusion with 
the comparison operator and to introduce the idea of variables as labels, not 
containers.  How about:

a2 --> a**2
a --> b --> c --> 0

A simple demo:

>>> c --> 1  # move label c, not a or b
>>> a,b,c
(0, 0, 1)

>I think some kind of a CTL approach would be especially good for a math 
>curriculum.  Maybe instead of 'Computer' TL, call it 'Computational' TL.

Good point.  That was actually the choice in Fletcher's article.  Kids will 
probably shorten it to computer, however.

>It's what algebra should be these days.  And if your CTL also RUNS, well, so 
>much the better.
>
>Most students, and probably most people, would read "2 + 2" as "2 plus 2", but 
>notice how much more mathematically and computationally effective it would be 
>if they could develop the habit of reading or thinking of "2 + 2" as "the sum 
>of 2 and 2" or "sum(2, 2)".  Think of the whole, the resulting value.
>
>And how about "2 + 3 * 4"?  Again, the typical reading would be "2 plus 3 
>times 4", and that's ambiguous, and so in math classes we have to talk about 
>'order of operations', and usually the only justification we give for 'order 
>of operations' is that we have to have some kind of social agreements in place 
>in order to avoid confusion.  Right?
>
>However, it is again more mathematically effective to read "2 + 3 * 4" as "the 
>sum of 2 and the product of 3 and 4", or,  sum(2, product(3, 4)).  No 
>ambiguity there!  And this is how you have to think when you hook chains of 
>functions together.  This kind of stuff could be done very early in the 
>curriculum.  Doesn't have to wait for either advanced math classes or computer 
>science.

It would be nice to avoid in CTL the complexities of precedence and 
associativity rules (very non-fundamental knowledge).  How about we just do all 
operations in the order they occur, unless you force a different order with 
parens.

2 + 3 * 4  same as (2 + 3) * 4
2 + 3 * 4 ** 5  same as ((2 + 3) * 4) ** 5

On the other hand, we don't want to teach something students have to unlearn 
when they get to a real language.  Since CTL is for CS0-level students, we 
should discourage complex expressions that rely on these rules, and not drill 
or test students on this particular choice.  It's just there to make things 
easy for beginners.

>Math teachers often forget, or are unaware, that the ordinary arithmetic 
>operators are themselves functions.  I think it would be good for math classes 
>to explore this kind of functional composition for very simple ideas.
>
>By the way, we started studying sequences today in class.  What's a really 
>good Pythonic tool for studying sequences ---> generators!

Should we include generators in CTL?  Seems like students at this level should 
have graduated to a full-featured language.  Might be fun just as an 
intellectual exercise to play around with an "advanced CTL".  I've got some 
ideas for unifying all methods (class, static, normal) into one simple syntax, 
identical to functions.

Here's what I have so far.  Shall I put this up on a wiki, so we can all hack 
at it?

=== CTL Syntax ===

# operators & expressions
2 + 3 * 4       same as (2 + 3) * 4
2 + 3 * 4 ** 5  same as ((2 + 3) * 4) ** 5

# variables as labels applied to objects
a2 --> a**2
a --> b --> c --> 0

# built in functions
min(), max(), sum(), and sqrt()

# custom functions
f(a,b): (a+b)/2
f(a,b): (a+b)/(2*pi)      # with an "outside" variable

# functions with multiple statements
f(a,b): a2 = a**2; b2 = b**2; return (a2 + b2)/2

# same but more readable
f(a,b):
        a2 = a**2
        b2 = b**2
        return (a2 + b2)/2

# function returning a tuple (sneaky introduction to objects)
f(a,b): (a, (a+b)/2, b)

>>> f(2,4)
(2, 3.0, 4)

# logic
<  <=  == >=  > !=  if  elif  else
if a<b,(b-a); else (a-b)

# control flow with multiple statements
f(a,b): if a<(b-5), return a; if (b-5)<=a<=(b+5), return (a+b)/2; return b

# recursion
f(a,b): if a<b, f(a, b-a); elif b<a, f(a-b, b); else (a,b)

factorial(n):
    if n < 2 ---> 1
    else ---> n*factorial(n-1)


************************************************************     *
* David MacQuigg, PhD    email: macquigg at ece.arizona.edu   *  *
* Research Associate                phone: USA 520-721-4583   *  *  *
* ECE Department, University of Arizona                       *  *  *
*                                 9320 East Mikelyn Lane       * * *
* http://purl.net/macquigg        Tucson, Arizona 85710          *
************************************************************     *


_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to