Code Comments
Programming Forum and web based access to our favorite programming groups.I've been following along with an article from an IBM site on using functions as arguments, function-generating functions, and anonymous functions. Anyway, I tried a simple program in LispMe yesterday and got an error, while the same input fed into mit-scheme running on Linux worked just fine. Warning: I am very new to Scheme. Here are the lines of code: (define square (lambda (x) (* x x))) (define my-test-data '(1 2 3 4 5 6)) (define list-of-squares (map square my-test-data)) I put these into a memo. Next I started up LispMe and loaded the standard library. Finally I loaded my memo, but LispMe returns a runtine error: Sucked into black hole. That's bad, right? I try typing (display list-of-squares) on the input line and press the eval key, and the system returns a compiler error: Undefined name list-of-squares. However, I do find list-of-squares on the Symbols drop down list. Any suggestions would be appreciated. TIA, Steve
Post Follow-up to this messagejsgrahamus@yahoo.com wrote: > I've been following along with an article from an IBM site on using > functions as arguments, function-generating functions, and anonymous > functions. Anyway, I tried a simple program in LispMe yesterday and got > an error, while the same input fed into mit-scheme running on Linux > worked just fine... It works just fine in PLT Scheme as well, so I suspect LispMe doesn't quite conform to standard Scheme semantics. As I don't have a Palm I can't test this code, but perhaps the following change will work: (define list-of-squares (lambda () (map square my-test-data))) This defines list-of-squares as a function that returns a list of squares, and so delays accessing the definitions which seems to cause the problems. HTH, Noel
Post Follow-up to this messagejsgrahamus@yahoo.com wrote: > I've been following along with an article from an IBM site on using > functions as arguments, function-generating functions, and anonymous > functions. Anyway, I tried a simple program in LispMe yesterday and got > an error, while the same input fed into mit-scheme running on Linux > worked just fine. Warning: I am very new to Scheme. > > Here are the lines of code: > > (define square (lambda (x) (* x x))) > (define my-test-data '(1 2 3 4 5 6)) > (define list-of-squares (map square my-test-data)) > > I put these into a memo. Next I started up LispMe and loaded the > standard library. Finally I loaded my memo, but LispMe returns a > runtine error: Sucked into black hole. That's bad, right? > > I try typing (display list-of-squares) on the input line and press the > eval key, and the system returns a compiler error: Undefined name > list-of-squares. However, I do find list-of-squares on the Symbols drop > down list. > > Any suggestions would be appreciated. > > > TIA, Steve
Post Follow-up to this messagejsgrahamus@yahoo.com wrote: > I've been following along with an article from an IBM site on using > functions as arguments, function-generating functions, and anonymous > functions. Anyway, I tried a simple program in LispMe yesterday and got= > an error, while the same input fed into mit-scheme running on Linux > worked just fine. Warning: I am very new to Scheme. >=20 > Here are the lines of code: >=20 > (define square (lambda (x) (* x x))) > (define my-test-data '(1 2 3 4 5 6)) > (define list-of-squares (map square my-test-data)) >=20 > I put these into a memo. Next I started up LispMe and loaded the > standard library. Finally I loaded my memo, but LispMe returns a > runtine error: Sucked into black hole. That's bad, right? On <http://www.lispme.de/lispme/doc/lm_err.htm> you can see what the various messages mean: [R9] Sucked into black hole During evaluation of the local definitions in a letrec form, the value of one of the variables just being bound was accessed. An access to the value of any variable in the binding list will cause this error while the expressions to initialize these variables are evaluated. These variables may be used inside a lambda or delay special form, as in this case their value will not be needed during evaluation of these forms. At first this seems strange, but if you examine how LispMe interprets the define, this begin to make sense: <http://www.lispme.de/lispme/doc/lm_...htm#desc_define> In LispMe, every name used must have been defined before, so every variable used in expri must have been defined in the same binding group (see loading), in a previously loaded memo or manual definition before, or in the case of a local definition, in the enclosing construct. In fact, a group of definitions (like a memo to be loaded, or a list of definitions enclosed in a begin expression entered into the command line) or a group of local definitions is treated like a letrec binding group; this means that every name used in this group must have been defined before or must be defined in this binding group (for mutual recursive definitions). This assures that every name can be statically resolved at compile time and there's no symbol table left to be checked at runtime. That is, top-level defines in LispMe behaves the same as internal defines does in "normal" Scheme implementations. > (define square (lambda (x) (* x x))) > (define my-test-data '(1 2 3 4 5 6)) > (define list-of-squares (map square my-test-data)) Try this in stead: (define square (lambda (x) (* x x)) (define my-test-data '(1 2 3 4 5 6)) (define list-of-squares (lambda () (map square my-test-data)) and then write (list-of-squares) in the REPL. --=20 Jens Axel S=F8gaard
Post Follow-up to this messageOn Sat, 09 Apr 2005 22:10:24 +0200, Jens Axel Søgaard wrote: > jsgrahamus@yahoo.com wrote: > > > On <http://www.lispme.de/lispme/doc/lm_err.htm> you can see what > the various messages mean: > > [R9] Sucked into black hole > > During evaluation of the local definitions in a letrec form, the > value of one of the variables just being bound was accessed. An > access to the value of any variable in the binding list will > cause this error while the expressions to initialize these > variables are evaluated. These variables may be used inside > a lambda or delay special form, as in this case their value > will not be needed during evaluation of these forms. > > At first this seems strange, but if you examine how LispMe > interprets the define, this begin to make sense: > > <http://www.lispme.de/lispme/doc/lm_...htm#desc_define> > > In LispMe, every name used must have been defined before, so > every variable used in expri must have been defined in the > same binding group (see loading), in a previously loaded memo > or manual definition before, or in the case of a local > definition, in the enclosing construct. In fact, a group of > definitions (like a memo to be loaded, or a list of definitions > enclosed in a begin expression entered into the command line) > or a group of local definitions is treated like a letrec binding > group; this means that every name used in this group must have > been defined before or must be defined in this binding group > (for mutual recursive definitions). This assures that every > name can be statically resolved at compile time and there's > no symbol table left to be checked at runtime. > > That is, top-level defines in LispMe behaves the same as internal > defines does in "normal" Scheme implementations. > > > Try this in stead: > > (define square (lambda (x) (* x x)) > (define my-test-data '(1 2 3 4 5 6)) > (define list-of-squares (lambda () (map square my-test-data)) > > and then write > > (list-of-squares) > > in the REPL. Thanks, this worked! I don't understand why the addition of the lambda made it work. I also don't understand why a (display list-of-squares) does not display the list of squares. Instead it yields [clos 0]. This is a little different from what I've been used to for the last many years. Steve Graham
Post Follow-up to this messageSteve Graham wrote: > On Sat, 09 Apr 2005 22:10:24 +0200, Jens Axel S=F8gaard wrote: >=20 >=20 > Thanks, this worked! I don't understand why the addition of the lambda= > made it work. I also don't understand why a (display list-of-squares) > does not display the list of squares. Instead it yields [clos 0]. >=20 > This is a little different from what I've been used to for the last man= y > years. Unlike a standard Scheme interpreter LispMe rewrites the original three defines to (letrec ((square (lambda (x) (* x x))) (my-test-data '(1 2 3 4 5 6)) (list-of-squares (map square my-test-data)) (list-of-squares)) The problem is that letrec specifies that one should be able to evaluate all the right hand sides without refering to the left hand sides. Since the right hand side of list-of-squares refer to my-test-data, without knowing the result of my-test-data. The revised version is rewritten to: (letrec ((square (lambda (x) (* x x))) (my-test-data '(1 2 3 4 5 6)) (list-of-squares (lambda () (map square my-test-data))) list-of-squares) Now the right hand side is a closure, which can be evaluated without knowing the result of my-test-data. But, since list-of-squares now is function, just evaluating list-of-squares will give a function (represented as a closure). This explains why your (display list-of-squares) writes [clos 0]. To get the actual squares, you need to call the function as (list-of-squares). I.e. try (display (list-of-squares)) --=20 Jens Axel S=F8gaard
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.