Re: lisp function questions
- From: Pascal Bourguignon <spam@xxxxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 19:06:47 +0200
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"
.
- Follow-Ups:
- Re: lisp function questions
- From: dan655t
- Re: lisp function questions
- References:
- lisp function questions
- From: dan655t
- lisp function questions
- Prev by Date: Re: What's so great about lisp?
- Next by Date: Re: The joy of garbage collection
- Previous by thread: lisp function questions
- Next by thread: Re: lisp function questions
- Index(es):
Relevant Pages
|
|