Re: A critic of Guido's blog on Python's lambda

2006-05-07 Thread Tomasz Zielonka
Alex Martelli wrote:
 Tomasz Zielonka [EMAIL PROTECTED] wrote:

 Alex Martelli wrote:
  Having to give functions a name places no ceiling on expressiveness,
  any more than, say, having to give _macros_ a name.
 
 And what about having to give numbers a name?

 Excellent style, in most cases; I believe most sensible coding guides
 recommend it for most numbers -- cfr
http://en.wikipedia.org/wiki/Magic_number_(programming) , section
 magic numbers in code.

I was a bit unclear. I didn't mean constants (I agree with you on
magic numbers), but results of computations, for example

(x * 2) + (y * 3)

Here (x * 2), (y * 3) and (x * 2) + 3 are anonymous numbers ;-)

Would you like if you were forced to write it this way:

a = x * 2
b = y * 3
c = a * b

?

Thanks for your answers to my questions.

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-07 Thread Tomasz Zielonka
I V wrote:
 Monads are one of those parts of functional programming I've never really
 got my head around, but as I understand them, they're a way of
 transforming what looks like a sequence of imperative programming
 statements that operate on a global state into a sequence of function
 calls that pass the state between them.

This is a description of only one particular kind of monad - a state
monad. A generalisation of your statement would be something like this:
they're a way of writing what looks like a sequence of imperative
programming statements that, depending on the monad, can have certain
computational side-effects (like operating on a global state) in a
purely functional way. But this doesn't explain much. If you want to
know more, there are some pretty good tutorials on
http://www.haskell.org/.

 So, what would be a statement in an imperative language is an anonymous
 function that gets added to the monad, and then, when the monad is run,
 these functions get executed.

A monad is a type, it isn't run. The thing you run can be called a
monadic action. You don't add functions to a monad (in this sense), you
build a monadic action from smaller monadic actions, gluing them with
functions - here's where anonymous functions are natural.

 The point being, that you have a lot of small functions (one for each
 statement) which are likely not to be used anywhere else, so defining
 them as named functions would be a bit of a pain in the arse.

Exactly!

 Actually, defining them as unnamed functions via lambdas would be annoying
 too, although not as annoying as using named functions - what you really
 want is macros, so that what looks like a statement can be interpreted is
 a piece of code to be executed later.

Haskell has one such macro - this is the do-notation syntax. But it's
translation to ordinary lambdas is very straightforward, and the choice
between using the do-notation or lambdas with = is a matter of
style.

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-07 Thread Tomasz Zielonka
Alex Martelli wrote:
 Worst case, you name all your functions Beverly so you don't have to
 think about the naming

I didn't think about this, probably because I am accustomed to Haskell,
where you rather give functions different names (at the module top-level
you have no other choice). I just checked that it would work for nested
Beverly-lambdas (but could be quite confusing), but how about using more
then one lambda in an expression? You would have to name them
differently.

 but you also have a chance to use meaningful names (such as,
 presumably, zipperize_widget is supposed to be here) to help the
 reader.

[OK, I am aware that you are talking solely about lambdas in Python,
but I want to talk about lambdas in general.]

Sometimes body of the function is its best description and naming what
it does would be only a burden. Consider that the same things that you
place in a loop body in python, you pass as a function to a HOF in
Haskell. Would you propose that all loops in Python have the form:

def do_something_with_x(x):
...
do something with x
for x in generator:
do_something_with_x(x)

Also, having anonymous functions doesn't take your common sense away, so
you still have a chance.

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-06 Thread Tomasz Zielonka
Bill Atkins wrote:
 OK, my real question is: what features of Python make it scalable?

Let me guess: Python makes it easier to scale the application on
the features axis, and the approach to large-scale computation
taken by google makes Python's poor raw performance not so big
an issue, so it doesn't prevent the application from scaling
on the load and amount of data axes. I also guess that python
is often used to control simple, fast C/C++ programs, or even
to generate such programs.

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-06 Thread Tomasz Zielonka
Alex Martelli wrote:
 ``An unneeded feature cannot be added (elegantly) in future releases
 of the language'' is just as trivial and acceptable for the unneded
 feature ``allow ( as an ordinary single-character identifier'' as for
 the unneded feature ``allow unnamed functions with all the flexibility
 of named ones''.

You can't be seriously claiming that these two features are equally
(un)needed. Anonymous functions come from the foundations of computer
science - Lamda Calculus. They are just a natural step on the road to
higher level languages. There are useful programming techniques, like
monadic programming, that are infeasible without anonymous functions.
Anonymous functions really add some power to the language.

On the other hand, what do you get by allowing ( as an indentifier?

Significant whitespace is a good thing, but the way it is designed in
Python it has some costs. Can't you simply acknowledge that?

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-06 Thread Tomasz Zielonka
Alex Martelli wrote:
 Having to give functions a name places no ceiling on expressiveness,
 any more than, say, having to give _macros_ a name.

And what about having to give numbers a name?

 Yes, we are, because the debate about why it's better for Python (as a
 language used in real-world production systems, *SCALABLE* to extremely
 large-scale ones) to *NOT* be insanely extensible and mutable is a
 separate one -- Python's uniformity of style allows SCALABILITY of
 teams, and teams-of-teams

I think this kind of language scalability is most important for Google,
see below.

 if your need SCALE, well then, PYTHON IS SCALABLE, and will remain a
 *SIMPLE, CLEAN, LITTLE AND POWERFUL LANGUAGE* (letting nobody do
 anything INSANE to it;-) while scaling up to whatever size of project(s)
 you need (including systems so large...

But honestly, isn't this scalability a result of the data processing
model you use, which is language independent? My point is that when you
have such a well scaling data processing model, most languages scale
well on the data and load axes, so you pick your language based on
how well it scales on the teams and features axes.

You seem to claim that there is something in Python that lets it scale
well on data and load, and is not related to teams and features,
and also not related to Google's data processing model. Can you tell
me what it is?

Best regards
Tomasz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-17 Thread Tomasz Zielonka
[EMAIL PROTECTED] wrote:
 -- The states are lists of regular expressions
 -- where [a,b,..] means match a or b or...

 I haven't run or studied your program yet myself but what I had in mind
 was that the list of wc's are *all* to be excluded, so the list
 [wc1..wcn] is to correspond generating all tuples matching not(wc1 and
 .. and wcn).  Maybe you're already doing that. The wc's themselves
 could be logical statements among the 'primitive' wc's.  That's why I
 named it the 'wildcard exclusion problem'. It's a lot easier to specify
 a list of simpler wc's than create a long logical expression.

I missed any logical combination :-(

It would be quite easy to fix my first program, but I don't have the
time to do it right now.

Best regards
Tomasz

-- 
I am searching for programmers who are good at least in
(Haskell || ML)  (Linux || FreeBSD || math)
for work in Warsaw, Poland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-16 Thread Tomasz Zielonka
[EMAIL PROTECTED] wrote:
 The python code below generates a cartesian product subject to any
 logical combination of wildcard exclusions. For example, suppose I want
 to generate a cartesian product S^n, n=3, of [a,b,c,d] that excludes
 '*a*b*' and '*c*d*a*'. See below for details.

 CHALLENGE: generate an equivalent in ruby, lisp, haskell, ocaml, or in
 a CAS like maple or mathematica.

What is your goal? You want to learn or to cause a flamewar? ;-)

Anyway, I found the problem entertaining, so here you go, here is my
Haskell code. It could be shorter if I didn't care about performance and
wrote in specification style. It's not very efficient either, because it
will generate all lists matching the given patterns.

In GHCi you can test it by:

$ ghci
:l WildCartesian.hs
test

I apologise for the lack of comments.

88888888
module WildCartesian where

import Data.Set (Set)
import qualified Data.Set as Set
import Control.Monad
import Control.Exception (assert)
import Maybe
import List

data Pat a = All | Lit a deriving Show

generateMatching :: (Ord a) = Int - Set a - [Pat a] - [[a]]
generateMatching 0   _[]= [[]]
generateMatching 0   _(_:_) = []
generateMatching len alphabet (Lit x : ps)
| x `Set.member` alphabet =
[ (x : xs) | xs - generateMatching (len - 1) alphabet ps ]
| otherwise =
[ ]
generateMatching len alphabet (All : ps) =
[ (x : xs)
| x - Set.toList alphabet
, xs - unionSorted
(generateMatching (len - 1) alphabet ps)
(generateMatching (len - 1) alphabet (All : ps)) ]
`unionSorted`
generateMatching len alphabet ps
generateMatching _   _[] = []

generateNotMatching :: (Ord a) = [a] - Int - [[Pat a]] - [[a]]
generateNotMatching alphabet len patterns =
generateMatching len alphaSet [All]
`subtractSorted`
foldr unionSorted []
(map (generateMatching len alphaSet .  simplifyPat) patterns)
  where
alphaSet = Set.fromList alphabet

simplifyPat (All : All : ps) = simplifyPat (All : ps)
simplifyPat (p : ps) = p : simplifyPat ps
simplifyPat [] = []

joinSorted :: Ord a = [a] - [a] - [(Maybe a, Maybe a)]
joinSorted (x1:x2:_) _ | assert (x1  x2) False = undefined
joinSorted _ (y1:y2:_) | assert (y1  y2) False = undefined
joinSorted (x:xs) (y:ys) =
case x `compare` y of
LT - (Just x, Nothing) : joinSorted xs (y:ys)
EQ - (Just x, Just y)  : joinSorted xs ys
GT - (Nothing, Just y) : joinSorted (x:xs) ys
joinSorted (x:xs) [] = (Just x, Nothing) : joinSorted xs []
joinSorted [] (y:ys) = (Nothing, Just y) : joinSorted [] ys
joinSorted [] [] = []

unionSorted :: Ord a = [a] - [a] - [a]
unionSorted xs ys = catMaybes (map (uncurry mplus) (joinSorted xs ys))

subtractSorted :: Ord a = [a] - [a] - [a]
subtractSorted xs ys = catMaybes (map f (joinSorted xs ys))
  where
f (Just x, Nothing) = Just x
f _ = Nothing

test = do
t [1,2] 3 [[Lit 1, All, Lit 2]]
t ['a','b'] 3 [[Lit 'a', All, Lit 'b'], [Lit 'b', All, Lit 'a']]
t [1,2] 3 [[Lit 1, All, Lit 2], [Lit 2, All, Lit 1]]
  where
t a b c = do
putStrLn (concat (intersperse   [generateMatching, show a, show b, 
show c]))
mapM_ (putStrLn . (  ++) . show) (generateNotMatching a b c)
88888888

Best regards
Tomasz

-- 
I am searching for programmers who are good at least in
(Haskell || ML)  (Linux || FreeBSD || math)
for work in Warsaw, Poland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-16 Thread Tomasz Zielonka
Tomasz Zielonka wrote:
 putStrLn (concat (intersperse   [generateMatching, show a, show 
 b, show c]))

Minor correction: it should be generateNotMatching.

Best regards
Tomasz

-- 
I am searching for programmers who are good at least in
(Haskell || ML)  (Linux || FreeBSD || math)
for work in Warsaw, Poland
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programming challenge: wildcard exclusion in cartesian products

2006-03-16 Thread Tomasz Zielonka
Major correction (missing case):

Tomasz Zielonka wrote:
 generateMatching :: (Ord a) = Int - Set a - [Pat a] - [[a]]
 generateMatching 0   _[]= [[]]
  generateMatching 0   alphabet (All:ps) = generateMatching 0 alphabet ps
 generateMatching 0   _(_:_) = []

Best regards
Tomasz

-- 
I am searching for programmers who are good at least in
(Haskell || ML)  (Linux || FreeBSD || math)
for work in Warsaw, Poland
-- 
http://mail.python.org/mailman/listinfo/python-list