February 10, 2008
Posted by Eddie
Writing Scheme in Javascript I
This is an interesting little function that I ran across in Kent Dybvig’s The Scheme Programming Language. I thought I would give it a go in javascript. I wrote it out, and ran into two problems. First, I wasn’t returning anything from the anonymous-self-executing function, so it was being garbage collected, and the call to tell() would give an undefined (secret didn’t exist anymore). The second was that I initially declared secret without the var which gave it global scope. Took me a little while to figure these out, but since I haven’t looked at any javascript in months, I don’t feel so bad.
/* the original function from The Scheme Programming Language (define shhh #f) (define tell #f) (let ((secret 0)) (set! shhh (lambda (message) (set! secret message))) (set! tell (lambda () secret)))
(shhh "sally likes harry")
(tell) "sally likes harry"
secret Error: variable secret is not bound
*/
//the Javascript version of the same function
![]()
var shhh = false;
var tell = (function(){
var secret = 0;
shhh = function(message) { secret = message; }
return function() { console.info(secret); }
})();
shhh("harry likes sally");
console.info("tell: " + tell() );
![]()
No Comments Yet
You can be the first to comment!
Leave a comment
You must be logged in to post a comment.