Flat-schmat! [was Re: Python syntax in Lisp and Scheme]

Kenny Tilton ktilton at nyc.rr.com
Sun Oct 5 21:07:32 EDT 2003


Bengt Richter wrote:

> On Sun, 05 Oct 2003 12:27:47 GMT, Kenny Tilton <ktilton at nyc.rr.com> wrote:
> 
> 
>>
>>Andrew Dalke wrote:
>>
>>
>>>Still have only made slight headway into learning Lisp since the
>>>last discussion, so I've been staying out of this one.  But
>>>
>>>Kenny Tilton:
>>>
>>>
>>>>Take a look at the quadratic formula. Is that flat? Not. Of course
>>>>Python allows nested math (hey, how come!), but non-mathematical
>>>>computations are usually trees, too.
>>>
>>>
>>>Since the quadratic formula yields two results, ...
>>
>>I started this analogy, didn't I? <g>
>>
>>I expect most
>>
>>>people write it more like
>>>
>>>droot = sqrt(b*b-4*a*c)  # square root of the discriminate
>>>x_plus   = (-b + droot) / (4*a*c)
>>>x_minus = (-b - droot) / (4*a*c)
>>
>>Not?:
>>
>>(defun quadratic (a b c)
>>  (let ((rad (sqrt (- (* b b) (* 4 a c)))))
>>    (mapcar (lambda (plus-or-minus)
>>              (/ (funcall plus-or-minus (- b) rad) (+ a a)))
>>      '(+ -))))
>>	
>>:)
>>
> 
> So cluttered ;-)
> (and you evaluate a+a twice, why not (two-a (+ a a)) in the let ;-)

I like to CPU-binge every once in a while. :)

> 
> JFTHOI, a one-liner:
> 
> def quadr(a,b,c): return [op(-b, r)/d for r,d in [(sqrt(b*b-4*a*c),a+a)] for op in (add,sub)]

I like it! reminds me of COBOL's perform varying from by until. DEC 
Basics after Basic Plus also had statement modifiers:

    print x, y for x = 1 to 3 for y = 2 to 6 step 2 until <etc>

> 
> But that aside, don't you think the following 3 lines are more readable than your 5 above?
> 
>     def quadratic(a,b,c):
>         rad = sqrt(b*b-4*a*c); den =a+a
>         return (-b+rad)/den, (-b-rad)/den

Not bad at all. Now gaze upon true beauty:

(defun quadratic (a b c)
   (let ((rad (sqrt (- (* b b)
                       (* 4 a c)))) ;; much nicer than 4*a*c
         (den (+ a a)))
      (list (/ (+ (- b) rad) den)
            (/ (- (- b) rad) den)))))

(Not tested, and without my Lisp editor, parens likely off.)

As for line counting and squeezing semantics into the smallest screen 
area possible, (a) I have two monitors for a reason and (b) have you 
seen the language K?

:)

kenny







More information about the Python-list mailing list