Proposed new syntax

Chris Angelico rosuav at gmail.com
Fri Aug 18 10:59:36 EDT 2017


On Fri, Aug 18, 2017 at 11:46 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> Compare the well-known haskell tutorial
> http://learnyouahaskell.com/starting-out
> whose comprehension intro starts:
>
> | If you've ever taken a course in mathematics, you've probably run into set
> | comprehensions. They're normally used for building more specific sets out of
> | general sets. A basic comprehension for a set that contains the first ten even | natural numbers is
>
> | S = {2·x | x ∈ ℕ, x ≤ 10}
>
> Analogous thing shown at ghci prompt:
>
> | ghci> [x*2 | x <- [1..10]]
> | [2,4,6,8,10,12,14,16,18,20]

And what if you HAVEN'T taken a course in mathematics? What use is
this then? How would you teach this to a non-mathematician?

Pretty much everyone, at some point in their lives, will follow a set
of written instructions. Most commonly, a recipe for some sort of
food. It consists of a set of ingredients and a sequence of commands.
This translates well into a classic imperative style - for instance:

Ingredients.
100 g flour
250 g butter
1 egg

Method.
1. Sift the flour.
2. Put flour into mixing bowl.
3. Serve with caramel sauce.
4. Stir for 2 minutes.
5. Remove egg.
6. Rub the flour until sifted.
7. Stir for 2 minutes.
8. Fold the butter into the mixing bowl.
9. Pour contents of the mixing bowl into the baking dish.

Serves 1.

You might recognize that this is a part of a recipe for Fibonacci
numbers [1], but aside from a few tricky instructions like "remove
egg", it's no worse than a recipe for chocolate ripple cake [2]:

Ingredients
300g Chocolate Ripple biscuits
600mL thickened cream (for whipping)
1tsp vanilla essence
Sugar (“some”)
6-12 Lindor balls (to taste)
Rectangular plate/dish to work on (about the size of the packet of biscuits)

Directions
1. Collect ingredients. Read all the instructions and comprehend them.
2. Open the packet of chocolate biscuits. Find a broken one and eat it.
3. Taste a Lindor ball.
4. Whip the cream in a jug with plenty of room.
5. Add some sugar to the cream. If you have chocolate-flavoured sugar, use it.
... etc ...

Anyone who's worked with this kind of recipe will have no difficulty
understanding the concept of imperative code.

# Ingredients
import math
num = 42

# Mixing bowl
guess = 1

# Method
while not done:
    guess = num / guess
    if guess hasn't changed:
        done is True

print num to the screen

And apart from a small matter of syntax and a need to break down the
concept "hasn't changed" into another variable and a comparison, this
is valid Python code. Valid *imperative* code. It's the one thing that
practically everyone, not just those elite few who actually comprehend
higher mathematics, can easily grok.

ChrisA

[1] http://www.dangermouse.net/esoteric/chef_fib.html
[2] http://rosuav.github.io/shed/ChocRippleCake



More information about the Python-list mailing list