0
votes

I'm learning Scheme for the first time, and for practice I'm trying to write a program that returns a list of a specified length with all values equal to 1. I'm using the MIT/GNU Edwin editor on Windows 10. Here's the code I typed:

(define (listlength n)
  (if (= n 1)
      (list 1)
      (append (list 1) (listlength (- n 1)))))

(listlength 5)

I would hope for C-x C-e to return (1 1 1 1 1), but instead I get an unbound variable error:

;Unbound variable: listlength
;To continue, call RESTART with an option number:
;  (RESTART 3) => Specify a value to use instead of listlength.
;  (RESTART 2) => Define listlength to a given value.
;  (RESTART 1) => Return to read-eval-print level 1.
;Start debugger? (y or n):

The only reason I can think of is that it doesn't like me calling listlength in the definition of listlength, but that's supposed to be part of what makes Scheme Scheme, so??? i'm at a loss?? Thanks for any help you can give me!

1
If you look at the stack trace, is the error happening in the recursive call or in your original call?Barmar
Did you use C-x C-e before to evaluate the function definition? Or did you press C-x C-e only at the end of the (listlength 5) form? See the manual.Renzo

1 Answers

0
votes

You should check if you use C-x C-e at the end of the function. C-x C-e will evaluate the expression at the left of the cursor. Or you can use M-z ,which will evaluate the whole expression not matter where is the cursor. I am also a beginner of Scheme, and I hope the answer can help you!