Norman DeValliere <normandevalliere@earthlink.net> wrote:
+---------------
| My question regards this error for this code in MrEd-
| > (define (even-fibs n)
| (define (next k)
| (if (> k n)
| nil
| (let ((f (fib k)))
| (if (even? f)
| (cons f (next (+ k 1)))
| (next (+ k 1)))))))
| define-values: illegal use (not at top-level) in: (#%define-values (next)
| (#%lambda (k) (if (> k n) nil (let ((f (fib k))) (if (even? f) (cons f (next
| (+ k 1))) (next (+ k 1)))))))
| >
+---------------
I agree that the error message is less than helpful, but your code really
is incorrect [perhaps you have a typo?]. Here's a properly-indented[*]
version of what you wrote:
(define (even-fibs n)
(define (next k)
(if (> k n)
nil
(let ((f (fib k)))
(if (even? f)
(cons f (next (+ k 1)))
(next (+ k 1)))))))
Do you see anything missing?
Hint #1: Where is "next" first called?
Hint #2: Look at the error message MzScheme gives to this example. Why?
[Or equivalently, how is it like your code?]
> (define (foo x)
(define (bar y)
(+ x y)))
define-values: illegal use (not at top-level) in:
(#%define-values (bar) (#%lambda (y) ...
>
Hint #3: This example doesn't fail. How does it differ from the previous two?
> (define (fact n)
(define (aux i p)
(if (<= i 2)
p
(aux (- i 1) (* i p))))
(aux n 1))
> (fact 12)
239500800
>
-Rob
[*] Well, my preferred format, at least.
-----
Rob Warnock, 8L-855 rpw3@sgi.com
Applied Networking http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
2011 N. Shoreline Blvd. FAX: 650-964-0811
Mountain View, CA 94043 PP-ASEL-IA