Obfuscated oneliner 
Author Message
 Obfuscated oneliner

Quote:

>Hi!

>I have always wondered why under "Obfuscated oneliners" title in python FAQ
>crystal clear examples of functional style are listed. So here is one more,
>directly translated from Haskell (but in Haskell it is four times shorter):

>qs = lambda x: x and qs(filter(lambda x, x0=x[0]: x<x0, x[1:])) + \
>             x[:1] + qs(filter(lambda x, x0=x[0]: x>=x0, x[1:]))

>I hope it is evident what and how this does. ;-)

Great way to solve the problem in a truly recursive fashion! If you are
brave, try it with a VERY big list!!!

By the way, I'm trying to learn how do you e{*filter*} T.P., since I'd very
much like to see a LONG thread about functional programming, maybe
taking off from a purely functional implementation of ... tabnanny.py

You know, I've never been exposed to such clean languages like 'Lots of
Inhelegant and Stupid Parentheses', and therefore, from my old fashioned
f77 standpoint I still fail to understand what this whole f.p. hyped stuff
could do for me... Maybe get my first coffee in a subliminal fashion
straight from the monitor???

BTW, the f.p. stuff seems really interesting, but I'm unable to understand
anything longer than this qs stuff, and even more unable to think about a
single way to put such stuff to use...

<!-- more stuff to make Tim respond -->

In-a-make-it-simple-please-attitude-ly

<!-- sue me Tim -->

Servus,

lorenzo



Mon, 23 Oct 2000 03:00:00 GMT  
 Obfuscated oneliner

[Cyril Slobin]

Quote:
> I have always wondered why under "Obfuscated oneliners" title in
> Python FAQ crystal clear examples of functional style are listed.

Just guessing here, but maybe it's because people without a functional
background find functional idioms baffling <wink>?

Quote:
> So here is one more, directly translated from Haskell (but in
> Haskell it is four times shorter):

> qs = lambda x: x and \
>          qs(filter(lambda x, x0=x[0]: x<x0, x[1:])) + \
>          x[:1] + \
>          qs(filter(lambda x, x0=x[0]: x>=x0, x[1:]))

> I hope it is evident what and how this does. ;-)

Yes, it's very pretty!  Not obfuscated at all.  Slow as a castrated pig, too
<wink>.

This is obfuscated:

p=lambda n,s=2,np=lambda s,f:reduce(lambda x,y:x and y,map(lambda
  x,y:x%y,[s+1]*(s/2),range(2,s/2+2)))and s+1or f(s+1,f
  ):n>0and[s]+p(n-1,np(s,np))or[]

Although that's actually straightforward -- try "print p(50)" in the
unlikely case it's not self-evident <wink>.

[Lorenzo M. Catucci]

Quote:
> Great way to solve the problem in a truly recursive fashion! If
> you are brave, try it with a VERY big list!!!
> By the way, I'm trying to learn how do you e{*filter*} T.P.,

My wife has been wondering the same thing since '76.  OK, OK, that's a
lie -- I never got e{*filter*}d enough to wed to begin with <wink>.

Quote:
> since I'd very much like to see a LONG thread about functional
> programming,

It *is* worth learning how "functional minds" function, but this isn't the
place for it.  Python's functional features are few and comparatively
weak -- they're best viewed as minor conveniences in Python.  The FAQ for
comp.lang.functional is an excellent place to start, though, as is

        http://www.*-*-*.com/

The "Gentle Introduction to Haskell" paper is especially good at getting to
"the features" quickly, without getting bogged down in the f.p. community's
Holy Purity Wars that make Python's periodic Indentation Slugfests look like
... well, look like a bunch of Python people being characteristically polite
to each other <wink>.

Quote:
> maybe taking off from a purely functional implementation
> of ... tabnanny.py

Now *that's* exciting <wink>!

Quote:
> You know, I've never been exposed to such clean languages like
> 'Lots of Inhelegant and Stupid Parentheses', and therefore, from
> my old fashioned f77 standpoint I still fail to understand what
> this whole f.p. hyped stuff could do for me...

You started to answer your question already:  Cyril's forumulation of
quicksort is elegant and almost self-evident, and would be even more so in a
"real" functional language (~= one with syntax designed for functional
styles).  f.p. programs can be compact and easy to reason about.

An especially good f.p. language to look at for fortran-heads is SISAL (I
don't know whether that's still a living language, though), which has been
applied to substantial "supercomputer problems" with good results -- there's
even a SISAL version of the Livermore Loops, and at least at one time
SISAL's speed was competitive with optimized Fortran's on Cray machines.
SISAL programs were typically smaller and much clearer, too.

Alas, for all the effect they've had on "mainstream" language development,
I'm afraid functional programming languages *almost* may as well never have
existed.  They seem to appeal to a twist of mind that's relatively rare.
Worth learning, though!  Never hurts to learn another way of viewing
problems, and at least the simpler functional idioms *have* influenced the
design of "array languages" (including Python's own NumPy extension).

Here's another version of "p" above, so elegant *in concept* it could make
you puke.  You'll eventually bump into some version of this in a "lazy"
functional language, and then you can come back here to see how the elegance
"gets lost" when expressing it in Python (we're "faking" lazy infinite
lists, and nested lexical scoping, here):

fromn = lambda start: lambda n=start: (n, fromn(n+1))

def filtermults(n, generator):
    def inner(n=n, generator=generator):
        while 1:
            next, generator = generator()
            if next % n:
                return next, filtermults(n, generator)
    return inner

def sieve(generator):
    def inner(generator=generator):
        next, generator = generator()
        return next, sieve(filtermults(next, generator))
    return inner

def firstn(generator, howmany):
    answer = []
    for i in range(howmany):
        next, generator = generator()
        answer.append(next)
    return answer

n = 50
print p(n)
print firstn(sieve(fromn(2)), n)

In English, this says "to get a list of all primes, start with a list of all
integers from 2 onward, then:   output the first element of the list, remove
all its multiples from the rest of the list, and do the same thing with the
list that remains".  In "a real" functional language, the code can read a
lot more like the English; but in Python, we'd be much better off modeling
infinite-list generators as classes.

easy-exercise-for-the-reader<wink>-ly y'rs  - tim



Tue, 24 Oct 2000 03:00:00 GMT  
 Obfuscated oneliner

Quote:
> It *is* worth learning how "functional minds" function, but this isn't the
> place for it.  Python's functional features are few and comparatively
> weak -- they're best viewed as minor conveniences in Python.

Make that "minor annoyances" :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)



Tue, 24 Oct 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. LOGO-L> oneliners and home in mswlogo

2. -e oneliners with File.open

3. Oneliners?

4. GForth & Obfuscated Source Code

5. obfuscated Forth?

6. ANNOUNCE: 0th International Obfuscated Haskell Code Contest

7. Obfuscated Haskell

8. ANN: XS: Obfuscated programming language

9. Results of the 1993 Obfuscated Haskell Code Contest

10. Obfuscated Haskell Code Contest

11. Desperately seeking obfuscated C!!!!

12. obfuscated ruby contest

 

 
Powered by phpBB® Forum Software