theblacksquid
theblacksquid

Reputation: 33

Unbound variable error when loading a procedure from an external file

I'm using Chicken Scheme 4.9.0.1 on a Cloud9 hosted workspace, built from source.

I was trying it out with this (I mostly code with python, so I apologize for the weird parens syntax):

(define load-module
    (lambda (filepath) 
        (begin
            (load filepath)
        )
    )
)

(define print
    (lambda (command)
        (begin 
            (display command)
            (newline)
        )
    )
)

(load-module "../src/Basics.scm")
(print (exponent 5 2))

where exponent was:

(define (exponent num pow)
    (if (equal? pow 1)
        num
        (* num (exponent num (- pow 1))
        )
    )
)

But it gives me the following error:

Started /home/ubuntu/workspace/test.scm

CHICKEN
(c) 2008-2014, The Chicken Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.9.0.1 (stability/4.9.0) (rev 8b3189b)
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
bootstrapped 2014-06-07

; loading /home/ubuntu/workspace/project1/src/test.scm ...
; loading ../src/Basics.scm ...

Error: unbound variable: pow

        Call history:

        <eval>    [append] (cons item (reverse target))
        <eval>    [append] (reverse target)
        <eval>    [append] (reverse (cons item (reverse target)))
        <eval>    [append] (cons item (reverse target))
        <eval>    [append] (reverse target)
        <eval>    [append] (reverse (cons item (reverse target)))
        <eval>    [append] (cons item (reverse target))
        <eval>    [append] (reverse target)
        <eval>    [append] (reverse (cons item (reverse target)))
        <eval>    [append] (cons item (reverse target))
        <eval>    [append] (reverse target)
        <syntax>          (print (exponent 5 2))
        <syntax>          (exponent 5 2)
        <eval>    (print (exponent 5 2))
        <eval>    (exponent 5 2)
        <eval>    [exponent] (equal? pow 1)     <--

I tried the same procedure on a different scheme implementation (biwascheme, using their online REPL) and it worked. When I added the code directly into the file that i was working on without loading it from a separate file, then it works.

Why does it give that unbound variable error only when it loads from a separate file?

Upvotes: 3

Views: 2268

Answers (1)

Stephen Eilert
Stephen Eilert

Reputation: 1537

Works fine for me. Compiled Chicken with the same version on Ubuntu, down to the specific revision.

Your call history looks very strange. More specifically:

    <eval>    [append] (reverse target)
    <syntax>          (print (exponent 5 2))

The [append] (reverse target) portion looks weird. Your exponent function (as described) does not call that. Even assuming the history was wrong and that was actually inside the print function, I've checked library.scm and that history does not make sense either.

This leads me to believe that the Basics.scm file being loaded is, for some reason, not the one you are expecting and you ended up with a different, non-working version of exponent.

You could ask Chicken to display the contents of the file to make sure. Something such as:

(with-input-from-file "../src/Basics.scm"
  (lambda () 
    (display (read))))

The result should match the expected content.

Upvotes: 2

Related Questions