2

I'm trying to make a function where after 7 values are entered, they are put into an array and from that array, each element is added to another variable (avg).

I keep getting "The variable AVG is unbound". I don't see where the problem is.

(defun readTestScores()
    (let ((examScore 0)
          (avg 0)))
    (setf testScore (make-array 7))

    (format t "~&ENTER EXAM SCORE ~23T: ")
    (setq examScore(read))

    (format t "~&ENTER ALL TEST SCORES ~23T: ")
    (dotimes (i 7)
        (setf (aref testScore i) (read))
    )

    (dotimes (i 7)
        (setq avg (+ avg (aref testScore i)))
    )
)
(readTestScores)
3
  • What dialect is this? Common Lisp? Commented Apr 14, 2018 at 22:11
  • Yes it is Common Lisp (as far as I know, I just started) Commented Apr 14, 2018 at 22:57
  • 1
    Your let is closed right after you have bound the variables. TO be able to reference avg you have the rest of the code in the body of the let. The function doesn't return anything. What is it supposed to do? Commented Apr 15, 2018 at 0:42

2 Answers 2

1

Your use of let is incorrect. It should be:

(let ((examScore init-value)
      (avg 0))
    let-body)  

This way you would have declared and initialized two local variables, where examScore will be initialized to init-value. However, you have declared examScoreand defined its initial value as (avg 0), which would be the result of calling the function avg with a parameter of value 0. The avg function is probably not defined, since that was not your intention, therefore the unbound message.

Sign up to request clarification or add additional context in comments.

8 Comments

It is still unbound: Error: The variable AVG is unbound. 1 (continue) Try evaluating AVG again. 2 Specify a value to use this time instead of evaluating AVG. 3 Specify a value to set AVG to. 4 (abort) Return to level 0. 5 Return to top loop level 0. Type :b for backtrace or :c <option number> to proceed. Type :bug-form "<subject>" for a bug report template or :? for other options.
not sure what lisp you are using, but the line (setq avg (+ (avg) (aref testScore i))) should probably be: (setq avg (+ avg (aref testScore i))), avg is not a function without parameters but a local variable.
Same error.. updated the code btw. Running Common Lisp in Clozure CL.
@chemoBrain Now you have an extra parenthesis in your let clause (avg 0))**)** <- this one should cover until the end of the function, since you will use the avg variable afterwards, i.e. move the end parenthesis after the last dotimes is finished
Even if I take it apart as much as possible it is still unbound: (let examScore 0) (let avg 0)
|
0

Assigns sum of testScore array to avg variable.

(let avg (reduce #'+ testScore))

Edit:

(setq avg (/ (reduce #'+ testScore) 7))

2 Comments

No it doesn't.. let in CL has the format (let ((binding expression) ...) body ...) and thus in your code it seems avg shoudl ahve been a list of bindings and the body is (reduce...)
Than what is your suggestion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.