Re: lisp function questions
- From: Joe Marshall <jmarshall@xxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 14:17:41 -0400
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.
>
>
> 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))
> )
Part way there. Your function QROOT performs two calculations. It
calculates the first root, discards the value, then calculates the
second root, which it returns. Rather than discarding the first root,
you may wish to pass *both* roots to a function that constructs an
appropriate list.
>
> 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!
Not a bad first try! While I'm sure your teacher is looking for
something a bit different, let me point you at these Common Lisp
functions:
COUNT-IF
REDUCE
REMOVE-IF
Your teacher no doubt wants you to `manually' iterate down the list,
and there are many ways to do that, but if you are instinctively
thinking in terms of `summation', I think you should go with it
instead of having to re-learn it later.
.
- References:
- lisp function questions
- From: dan655t
- lisp function questions
- Prev by Date: Re: Low level C interaction (Specifically OpenGL)
- Next by Date: Re: Low level C interaction (Specifically OpenGL)
- Previous by thread: Re: lisp function questions
- Next by thread: Re: lisp function questions
- Index(es):
Relevant Pages
|