Re: lisp function questions



dan655t@xxxxxxxxx writes:

> Hi I am a newbie to lisp and need some help with my functions. I need
> to write a function that:
>
> takes 3 integer arguments representing the coefficients of ax^2 +bx +c
> and finds the solutions for ax^2 +bx +c = 0. The return value will be a
> a list with first element (-b + sqrt(b^2 - 4ac))/2a and second element
> (-b - sqrt(b^2 -4ac))/2a.
> If the roots are repeated, please list the root just once.

I don't know your math teached, but mine introduced a mathematical
variable named delta. Perhaps you could do the same in this function,
using LET:

(let ((delta (- (* b b) (* 4 a c))))
...)


> here is my work so far. It is lacking the second part which I'm looking
> for a way to implement.
>
> (defun QROOT (a b c)
> (/ (+ (- b) (sqrt ( - (* b b) (* 4 a c)))) (* 2 a))
> (/ (- (- b) (sqrt ( - (* b b) (* 4 a c)))) (* 2 a))
> )

And my same math teacher told us there was three cases:
(< 0 delta) where there are two distinct solutions
(= 0 delta) where there is only one double solution
(> 0 delta) where there are two distinct complex solutions, none real.

So perhaps you could use COND:

(COND
((< 0 delta) ...)
((= 0 delta) ...)
((> 0 delta) ...))

Read again your favorite tutorial about lisp, or the hyperspec.
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm

> My other function I am having trouble with is:
> a function that goes through a list and calculates the average of all
> numerica entries that appear somewhere inside the list.

> (defun average (m)
> (/ (+ m) (length m))
> )
>
> But I have to only count numeric entries. Also I do not think I am
> taking the sum of the list correctly. Any help would be greatly
> appreciated. Thanks!

Use "divide and conquer". You have two problems here:
1- select the numbers in the list
2- sum them.

To sum a list you could use the function REDUCE.
To select some kind of item from a list you can use the function DELETE-IF-NOT.


--
"You cannot really appreciate Dilbert unless you read it in the
original Klingon"
.



Relevant Pages

  • Re: teaching kids functional programming
    ... MT> One reason functional programming has not become more widespread, ... don't most of them use a 'partial sum' method (can't ... Lisp does it with an electronic processor, ... No rest parameter ...
    (comp.lang.lisp)
  • Re: Make 100 by using + - x / and 1~9
    ... It proceeds by collecting a sum of product terms P ... the highest power of p that can occur in the denominator ... the same fraction, or the denominator of other fractions). ... To keep the code simple (for the sake of those new to Lisp) ...
    (sci.math)
  • Re: Optimizing an algorithm used to rasterize polygons
    ... hold X,Y,COVER and DELTA, and stored them into a list. ... array is full, ... it is less obvious to sort ... Unless this is just an exercise in learning to write fast Lisp and not at all related to actually using the Antigraing technology, you have already invested more energy than it would have taken to carve out a DLL and learn a little CFFI -- don't forget, there are folks here and over at the CFFI list that can help with that, too. ...
    (comp.lang.lisp)
  • Re: where/forall question
    ... do a average of all the values that were with some delta of the ... real delta, array, current, sum ... simultaneously, so you can't assign to anything but an array inside a forall construct -- in particular, you can't do an iterated sum with a forall. ... As noted elsewhere, you _can_ do this in a simpler way by using the optional mask argument to the intrinsic sum() function, and using the intrinsic countfunction, but that requires calculating the condition twice, or doing something excessively clever and non-extensible with complex numbers. ...
    (comp.lang.fortran)
  • Re: Functions that create functions
    ... > instead have chosen to store a function in the value namespace. ... "normalise an array/list A so that the sum of components is 1" ... I'm currently on a trial version of Corman Lisp. ...
    (comp.lang.lisp)