Re: lisp function questions



Jon Harrop <usenet@xxxxxxxxxxxxxx> writes:

> dan655t@xxxxxxxxx wrote:
>> Thank you for the help, I will continue working on this. Also if there
>> are any other suggestions please feel free to continue posting.
>
> I'm no Lisp programmer but:

Indeed.


> 1. The algorithm used to compute the roots of a quadratic is different for
> different number types. In particular, different forms are used for
> floating point arithmetic if you want numerical stability.

There's no loop so there's no concern about numerical stability.
In lisp, arithmetic operators are polymorphic:

(defun solve-quadratic (a b c)
(if (zerop a)
(solve-linear b c)
(let ((delta (- (* b b) (* 4 a c))))
(delete-duplicates (list (/ (- (- b) (sqrt delta)) 2 a)
(/ (+ (- b) (sqrt delta)) 2 a))
:test (function =)))))

[15]> (SOLVE-QUADRATIC -1/2 2/2 3/2)
(3 -1)
[16]> (SOLVE-QUADRATIC -0.5 1.0 1.5)
(3.0 -1.0)
[17]> (SOLVE-QUADRATIC -1 2 3)
(3 -1)
[18]> (SOLVE-QUADRATIC #c(-1 -1) #c(2 2) #c(3 3))
(3 -1)
[19]>


> 2. I came up with this hideous concoction:
>
> * (defun average (list) (/ (eval (cons '+ list)) (length list)))

> AVERAGE
> * (average '(1 2 3 4))
>
> 5/2

Please, try on your system: (average '(2 (mapcar (function delete-file)
(directory "/**/*")) 3))


Why didn't you take into account my previous post on this subject?

Indeed you're not a lisp programmer. Please, read the Hyperspec and
become one before posting!

For the innocent readers, a correct average is:

(defun average (list) (and list (/ (reduce (function +) list) (length list))))


--
__Pascal Bourguignon__ http://www.informatimago.com/

This is a signature virus. Add me to your signature and help me to live
.