Functional programming

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 4 16:18:24 CET 2014


On Tue, 04 Mar 2014 13:30:04 +0000, BartC wrote:

> "Steven D'Aprano" <steve at pearwood.info> wrote in message
> news:53159540$0$2923$c3e8da3$76491128 at news.astraweb.com...
> 
>> It's that "explicitly" part that doesn't follow. Having to manage types
>> is the most tedious, boring, annoying, *unproductive* part of languages
>> like Java, C and Pascal. Almost always, you're telling the compiler
>> stuff that it can work out for itself.
> 
> Isn't creating classes in Python similar to creating types elsewhere?

Depends on the type: I suppose you can draw an analogy between records or 
structs and classes with no methods.

But I'm not talking about creating types, I'm talking about type 
declarations.

int x=2;  # 2 is an int? Who would have guessed!


>> In the same way that managing jumps for GOTO has been automated with
>> for loops, while, etc., and managing memory has been automated, there's
>> no good reason not to allow the compiler to manage types. Dynamically
>> typed languages like Python do so at runtime. Type inference simply
>> allows statically typed languages to do the same only at compile time.
> 
> Eliminating 'goto' is simple code-generation logic; type inference is
> more of an art.

Type inference is nothing like an art. It's a mathematically provable 
correct algorithm from the lambda calculus.

http://en.wikipedia.org/wiki/Hindley%E2%80%93Milner

Unfortunately the wikipedia page above appears to be complete 
gobbledygook if you aren't an expert in the lambda calculus, which I 
certainly am not, so I'm not even going to try to explain how it works. 
But there is no *guessing* involved, no heuristics which only sometimes 
work. There are certain assumptions involved, e.g. that the type of 
something is, in the absence of a declaration otherwise, the *most* 
general thing it could be (e.g. "it's an integer" rather than "it's an 
integer between 3 and 27"). But they're reasonable, practical assumptions.


> But declaring variables is not just about specifying a type; it
> registers the name too so that misspelled names can be picked up very
> early rather than at runtime (and that's if you're lucky).

You don't need to have static typing to have declared variables. The two 
are independent. E.g. one might have a system like Python, except you 
have to declare your variables before using them:

global x
local a
a = x+1
...

Or a system like (ancient) BASIC, where the type of the variable is given 
by the name. E.g. X is a numeric variable, and X$ is a string variable. 
There's no need for a separate declaration, because the presence or 
absence of the $ sign tells the interpreter whether it is a number or 
string variable. Perl, PHP and many other languages also use sigils.

Having to declare variables, and having to declare their type, are 
independent.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/



More information about the Python-list mailing list