merits of Lisp vs Python

Slawomir Nowaczyk slawomir.nowaczyk.847 at student.lu.se
Tue Dec 12 18:12:09 EST 2006


On Sat, 09 Dec 2006 14:57:08 -0500
Bill Atkins <atkinw at rpi.edu> wrote:

#> In Python, I would need to do something like:
#> 
#>   control_code = connection.read_next_control_code()
#>   if control_code == +break+:
#>     connection.kill()
#>     throw blah
#>   else if control_code == +status+:
#>     connection.send_status_summary()
#>   else if control_code == +noop+ || control_code == +keep_alive+:
#>   else:
#>     error "CONTROL_CODE fell through conditional cascade; was not one of +BREAK+, +STATUS+, +NOOP+, +KEEP_ALIVE+"

Hardly.

I mean, well, possibly, *you* might need to, but others might just do
things like this:

def main():
    connection = "+break+"
    def kill():
        print "kill",connection
        raise "broken-by-client"
    def status():
        print "status"
    def do_nothing():
        pass
    dispatch = {"+break+":kill,"+status+":status,"+noop+":do_nothing,"+keep-alive+":do_nothing}
    dispatch[connection]()

When you compare the above with your lisp version

#>   (let ((control-code (read-next-control-code connection)))
#>     (ecase control-code
#>       (+break+
#>         (kill-connection connection)
#>         (throw :broken-by-client))
#>       (+status+
#>         (send-status-summary connection))
#>       ((+noop+ +keep-alive+))))
#>   ;; +X+ indicates a constant

you may be able to see that they are pretty similar. Sure, Python does
not have fully functional lambda (which I, personally, consider to be a
drawback, but I understand that it is -- objectively -- a valid design
decision) so one needs to define functions for handling each case, but
there are some benefits to this approach as well.

#> To change what control codes you want to check for, you need to add
#> conditionals

Just as you need to add ecase branches...

#> for them and keep the error text relevant. The reality is that a
#> computer could be doing this for you, leaving your code simpler and
#> more easily changed.

Not really. If you care about the error message, you can use a subclass
of a dictionary instead and provide the correct behaviour when key is
not found.

#> Now someone will complain that the ECASE code means nothing until I
#> understand ECASE.  Yep.  But once you understand ECASE, you can look
#> at that code and, *at a glance*, see how control flows through it.  In
#> the equivalent Python code, I need to walk through each conditional
#> and make sure they're all following the same pattern.  If you're not
#> convinced, extend the example to 12 different control codes.

What you say is right, but only for bad Python code. Want to bet I can
write bad Lisp which will be just as unmaintainable?

PS. Good sigmonster, have a cookie!

-- 
 Best wishes,
   Slawomir Nowaczyk
     ( Slawomir.Nowaczyk at cs.lth.se )

Get the facts first - you can distort them later!




More information about the Python-list mailing list