From: Steve Heller
Subject: Getting average without Recursion only with Prog
Date: 
Message-ID: <390522df.9424311@enews.newsguy.com>
I am trying to get a program like this to work and can not, would
anyone have one, so I can see how to do it.  I am having a real hard
time with it.  I wrote a program and dont know why it isnt working.
There seems to be a lot of problems.  Please Help.
Thanks Scott

From: Donald Fisk
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <3905D269.1C93BCDE@inthan.be>
Steve Heller wrote:

> I am trying to get a program like this to work and can not, would
> anyone have one, so I can see how to do it.  I am having a real hard
> time with it.  I wrote a program and dont know why it isnt working.
> There seems to be a lot of problems.  Please Help.

I don't mind you asking for help with your homework, as long as
you post what you've done so far.   I do have a problem with the
idiot who set this as a student assignment -- prog is, to put it mildly,

considered harmful and you would learn nothing but bad things by
doing it that way.   Something along the lines of

(defun average (nums) (/ (apply #'+ nums) (length nums)))

works fine but is not the answer you've been asked for.

> Thanks Scott

Le Hibou (ma propre opinion)

--
"part of any serious QA is removing perl code the same
way you go over a dilapidated building you inherit to
remove chewing gum and duct tape and fix whatever was
kept together for real" -- Erik Naggum
From: Joseph Dale
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <390512FA.9A48ABE2@uclink4.berkeley.edu>
Steve Heller wrote:
> 
> I am trying to get a program like this to work and can not, would
> anyone have one, so I can see how to do it.  I am having a real hard
> time with it.  I wrote a program and dont know why it isnt working.
> There seems to be a lot of problems.  Please Help.
> Thanks Scott

Please post what you've written so far, and we'd be glad to help. It's a
matter of basic arithmetic operators, so you're probably trying to do
something more complex than you need to be.

Joe
From: Steve Heller
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <3905b6f4.47239280@enews.newsguy.com>
On Tue, 25 Apr 2000 06:37:26 GMT, Joseph Dale
<·····@uclink4.berkeley.edu> wrote:

>Steve Heller wrote:
>> 
>> I am trying to get a program like this to work and can not, would
>> anyone have one, so I can see how to do it.  I am having a real hard
>> time with it.  I wrote a program and dont know why it isnt working.
>> There seems to be a lot of problems.  Please Help.
>> Thanks Scott
>
>Please post what you've written so far, and we'd be glad to help. It's a
>matter of basic arithmetic operators, so you're probably trying to do
>something more complex than you need to be.
>
>Joe
here is my code,  It seems to be way off. PLEASE HELP

(defun avgfunct (l)
(prog (sum avg new cnt fnl)
(setq sum 0)
(setq avg 0)
loop
(setq sum (+ (car l) sum))
(not (null l))(setq cnt (+ 1 cnt))
(setq avg (/ sum cnt))
(cons (car l)(setq new))
(setq l(cdr l))
(cond
(null l)(>(car new)(avg))(cons (car new))
(setq fnl)
(setq new (cdr new))
((null new)return fnl)
(go loop)
)
)
)
From: Barry Margolin
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <iZjN4.29$my3.582@burlma1-snr2>
In article <·················@enews.newsguy.com>,
Steve Heller <·······@optonline.net> wrote:
>here is my code,  It seems to be way off. PLEASE HELP

Please indent your code in the future.  I've done it for you.

You seem to be totally lost in basic Lisp syntax.

>(defun avgfunct (l)
>  (prog (sum avg new cnt fnl)

FYI, PROG is almost never used these days; it exists in Common Lisp mainly
for compatibility with earlier Lisps, such as Maclisp.  LET is usually used
to introduce local variables.  For iterating, you should use LOOP or one of
the high-level iteration macros like DOLIST (you could also use one of the
mapping functions, but in your application this would then require a lambda
expression, which would complicate things more than I expect you're ready
for).

>     (setq sum 0)
>     (setq avg 0)
>     loop
>     (setq sum (+ (car l) sum))
>     (not (null l))(setq cnt (+ 1 cnt))

You're not doing anything with (not (null l)).  Should there be an IF
around here?

>     (setq avg (/ sum cnt))

Why do you need to do this each time through?  Why not wait until the end
and just divide once?

>     (cons (car l)(setq new))

You're creating a cons, but not doing anything with it.  And SETQ requires
two (or any even number) arguments.

>     (setq l(cdr l))
>     (cond
>      (null l)(>(car new)(avg))(cons (car new))

I am totally unable to figure out what you're trying to do in the above
line.  First of all, it's not correct COND syntax -- you need another level
of nesting.  Second, did you intend to AND or OR the NULL and > tests?  If
not, what's going on?  Third, why are you calling AVG as a function, when
it's actually a variable?  Fourth, CONS requires two arguments.  Fifth,
what's supposed to be in NEW and why are you comparing it to AVG?

>      (setq fnl)

Again, SETQ requires two arguments.

>      (setq new (cdr new))

What was supposed to be in NEW previously?

>      ((null new)return fnl)

The syntax of RETURN is (RETURN fnl).

>      (go loop)
>      )
>     )
>  )

Here's a simple implementation:

(defun avgfunct (l)
  (let ((count 0)
        (total 0))
    (dolist (item l)
      (incf count)
      (incf total item))
    (if (zerop count) ;; avoid divide by 0 for empty list
        0
        (/ total count))))

And if I'm not worried too much about performance, I might write it as:

(defun avgfunct (l)
  (let ((count (length l)))
    (if (zerop count)
        0
        (/ (reduce #'+ l) count))))

This makes two passes over the input list (once for LENGTH, and again for
REDUCE), but if the list isn't too long it's probably not a big deal.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Barry Margolin
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <7hiN4.15$my3.463@burlma1-snr2>
In article <················@enews.newsguy.com>,
Steve Heller <·······@optonline.net> wrote:
>I am trying to get a program like this to work and can not, would
>anyone have one, so I can see how to do it.  I am having a real hard
>time with it.  I wrote a program and dont know why it isnt working.
>There seems to be a lot of problems.  Please Help.

It's interesting that you emphasize "without Recursion", since in all the
hundreds of times I've computed averages I don't think I've ever considered
using recursion in the first place!  The average is simply the division of
the total of all the numbers in the sequence by the length of the
sequence.  Which part of this are you having trouble with?

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Kragen Sitaker
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <37rN4.1146$%i.244288@news-east.usenetserver.com>
In article <················@burlma1-snr2>,
Barry Margolin  <······@genuity.net> wrote:
>It's interesting that you emphasize "without Recursion", since in all the
>hundreds of times I've computed averages I don't think I've ever considered
>using recursion in the first place!

One would assume it's because it's part of the assignment.  :)
-- 
<······@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
The Internet stock bubble didn't burst on 1999-11-08.  Hurrah!
<URL:http://www.pobox.com/~kragen/bubble.html>
The power didn't go out on 2000-01-01 either.  :)
From: Johan Kullstam
Subject: Re: Getting average without Recursion only with Prog
Date: 
Message-ID: <ud7nbffm2.fsf@res.raytheon.com>
·······@optonline.net (Steve Heller) writes:

> I am trying to get a program like this to work and can not, would
> anyone have one, so I can see how to do it.  I am having a real hard
> time with it.  I wrote a program and dont know why it isnt working.
> There seems to be a lot of problems.  Please Help.
> Thanks Scott

try a mapping iterator like MAPC.  DOLIST is probably better but what
the heck.

(defun average (numlist)
  (let ((sum 0)
        (count 0))
    (mapc #'(lambda (n)
               (incf sum n)
               (incf count))
          numlist)
    (if (zerop count)
        nil
      (/ sum count))))

-- 
johan kullstam l72t00052